有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java编辑XML样式表更改Android中的按钮

我正在开发一个Android应用程序,我决定改变整个应用程序的颜色,所以我使用了一个xml样式表。这改变了我的颜色,但没有改变我的EditText、TextView和按钮颜色,因此我必须为每个元素创建一个单独的样式表,然后从我使用的主颜色表中调用这些样式表

然而,一旦我这样做了,颜色对这三个小部件起到了适当的作用,但是盒子缩小了,破坏了我们的整个布局。我们认为这是因为我们已经覆盖了这些默认样式,但我认为它的编写方式,我们只是覆盖了颜色选项

我想知道如何更改EditText、TextView和Button的颜色,但不更改它们的格式

风格。我们项目的xml:

<resources xmlns:安卓="http://schemas.安卓.com/apk/res/安卓">

<color name="oldblue">#33b5e5</color>
<color name="gungrey">#9C9C9C</color>
<color name="textGold">#ffe27e</color>
<color name="backgroundBlack">#000000</color>
<color name="buttonGrey">#222222</color>


<!--
    Base application theme, dependent on API level. This theme is replaced
    by AppBaseTheme from res/values-vXX/styles.xml on newer devices.
-->
<style name="AppBaseTheme" parent="安卓:Theme.Light">
    <!--
        Theme customizations available in newer API levels can go in
        res/values-vXX/styles.xml, while customizations related to
        backward-compatibility can go here.
    -->
</style>

<!-- Application theme. -->
<style name="AppTheme" parent="AppBaseTheme">
    <!-- All customizations that are NOT specific to a particular API-level can go here. -->
            <item name="安卓:textColor">@color/textGold</item>
            <item name="安卓:windowBackground">@color/backgroundBlack</item>
            <item name="安卓:colorBackground">@color/backgroundBlack</item>
            <item name="安卓:editTextStyle">@style/TextEditStyle</item>
            <item name="安卓:textViewStyle">@style/TextViewStyle</item>
            <item name="安卓:buttonStyle">@style/OurButtonStyle</item>
</style>


<!-- Allows us to edit the EditText field -->
<style name="TextEditStyle" parent="安卓:Widget.EditText"> 
    <item name="安卓:background">#e3e3e3</item> 
</style>

<!-- Allows us to edit the TextView words (much of the app text) -->
<style name="TextViewStyle" parent="安卓:Widget.TextView">
    <item name="安卓:textColor">@color/textGold</item>
</style>

<!-- Allows us to edit the Buttons in the app -->
<style name="OurButtonStyle" parent="安卓:Widget.Button">
    <item name="安卓:textColor">@color/textGold</item>
    <item name="安卓:background">@color/buttonGrey</item>
    <item name="安卓:padding">10dp</item>
    <!-- This line here helped us fix the issue with the buttons colliding with the textboxes, but ideally, we want to change a single thing and fix all the widgets and not have to use padding here -->


</style>    


</resources>

我们的应用程序在所有页面上使用线性布局。如果可能的话,我们希望保持这种方式。 请让我知道,如果您需要查看任何屏幕截图或个人网页的xml代码。不过,他们都犯了同样的错误

谢谢你能给我的帮助


共 (2) 个答案

  1. # 1 楼答案

    当您修改ButtonEditTextbackground属性时,您正在替换已存在的整个背景,这就是您丢失所有“格式”的原因

    因此,对于Button,您看到的默认按钮是实际图像

    例如,btn_default_normal_holo.9.png位于(sdk路径)\platforms\android-19\data\res\drawable xhdpi中

    btn_default_normal_holo.9.png

    一种方法是创建9 patch images来创建您的背景。你可以参考Basim Sherif here的答案。在这个问题上,它问了一个与你类似的问题。因此,在这种情况下,您可以使用现有图像并修改颜色

    否则,您将需要设置自己的样式(可绘图项和选择器)以“匹配”原始格式或使用自己的格式

    另外请注意,由于您将按钮的背景设置为颜色,因此不再有任何选择器(如在选择按钮时更改颜色)

  2. # 2 楼答案

    不要使用android:,而是尝试用@android:style/作为父引用的前缀