繁体   English   中英

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

[英]Editing XML Style Sheets Changes Buttons in Android

我正在开发一个Android应用程序,并决定要更改整个应用程序的颜色,因此我使用了xml样式表。 这改变了我的颜色,但是并没有改变我的EditText,TextView和Button的颜色,因此我必须为每个元素制作一个单独的样式表,然后从我使用的主颜色表中调用这些表。

但是,一旦我这样做,颜色就可以适合这三个小部件,但是盒子缩小并破坏了我们的整个布局。 我们认为这是因为我们已经覆盖了这些样式的默认样式,但是我认为它的编写方式只是覆盖了颜色选项。

我想弄清楚如何更改EditText,TextView和Button的颜色,但不更改其格式。

我们的项目的Styles.xml:

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

<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="android: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="android:textColor">@color/textGold</item>
            <item name="android:windowBackground">@color/backgroundBlack</item>
            <item name="android:colorBackground">@color/backgroundBlack</item>
            <item name="android:editTextStyle">@style/TextEditStyle</item>
            <item name="android:textViewStyle">@style/TextViewStyle</item>
            <item name="android:buttonStyle">@style/OurButtonStyle</item>
</style>


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

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

<!-- Allows us to edit the Buttons in the app -->
<style name="OurButtonStyle" parent="android:Widget.Button">
    <item name="android:textColor">@color/textGold</item>
    <item name="android:background">@color/buttonGrey</item>
    <item name="android: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代码,请告诉我。 他们都犯了同样的错误。

谢谢你提供的所有帮助!

代替android: :,尝试在父引用@android:style/加上@android:style/

修改ButtonEditTextbackground属性时,将替换已经存在的整个背景,这就是为什么丢失所有“格式”的原因。

因此,使用Button ,您看到的默认按钮是实际图像。

例如, btn_default_normal_holo.9.png位于(路径到SDK)\\平台\\机器人-19 \\数据\\水库\\抽拉-xhdpi

btn_default_normal_holo.9.png

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

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

还要注意,由于您将按钮的背景设置为一种颜色,因此不再有选择器(例如,在选择按钮时更改颜色)。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM