简体   繁体   English

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

[英]Editing XML Style Sheets Changes Buttons in Android

I'm working on an Android app and decided I wanted to change the colors across the whole app, so I used an xml style sheet. 我正在开发一个Android应用程序,并决定要更改整个应用程序的颜色,因此我使用了xml样式表。 This changed my colors, but it didn't change my EditText, TextView, and Button colors, so I had to make an individual style sheet for each element and then call those sheets from within the main color sheet I was using. 这改变了我的颜色,但是并没有改变我的EditText,TextView和Button的颜色,因此我必须为每个元素制作一个单独的样式表,然后从我使用的主颜色表中调用这些表。

However, once I did that, the colors acted appropriately for the three widgets, but the boxes shrunk and disrupted our entire layout. 但是,一旦我这样做,颜色就可以适合这三个小部件,但是盒子缩小并破坏了我们的整个布局。 We believe it's because we had overridden the default styles for those, but I thought the way it was written, we were merely overriding the color options. 我们认为这是因为我们已经覆盖了这些样式的默认样式,但是我认为它的编写方式只是覆盖了颜色选项。

I'd like to figure out how to change the colors of EditText, TextView, and Button, but not change their formatting. 我想弄清楚如何更改EditText,TextView和Button的颜色,但不更改其格式。

The Styles.xml for our project: 我们的项目的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>

Our app is using Linear Layout on all of our pages. 我们的应用程序在所有页面上都使用线性布局。 We would like to keep it this way if possible. 如果可能,我们希望保持这种方式。 Please let me know if you need to see any screenshots or xml code of the individual pages. 如果您需要查看各个页面的任何屏幕截图或xml代码,请告诉我。 They're all having the same error, though. 他们都犯了同样的错误。

Thanks for any help you can give! 谢谢你提供的所有帮助!

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

When you're modifying the background attribute for a Button or EditText , you're replacing the entire background that already exists which is why you're losing all the "formatting". 修改ButtonEditTextbackground属性时,将替换已经存在的整个背景,这就是为什么丢失所有“格式”的原因。

So with a Button , the default buttons you see are actual images. 因此,使用Button ,您看到的默认按钮是实际图像。

For example, btn_default_normal_holo.9.png located in (path-to-sdk)\\platforms\\android-19\\data\\res\\drawable-xhdpi 例如, btn_default_normal_holo.9.png位于(路径到SDK)\\平台\\机器人-19 \\数据\\水库\\抽拉-xhdpi

btn_default_normal_holo.9.png

One way is to create 9 patch images to create your background. 一种方法是创建9 patch images来创建背景。 You can refer to the answer by Basim Sherif here . 您可以在此处参考Basim Sherif的答案。 In that question, it asks a similar question to yours. 在该问题中,它会问一个与您类似的问题。 So in this case you can use the existing images and modify the colors. 因此,在这种情况下,您可以使用现有图像并修改颜色。

Otherwise, you will need to set your own styles (drawables and selectors) to "match" the original formatting or using your own formatting. 否则,您将需要设置自己的样式(可绘制和选择器)以“匹配”原始格式或使用自己的格式。

Also note that since you set the background of your buttons to a color, there aren't any selectors anymore (like changing the color when the button is selected). 还要注意,由于您将按钮的背景设置为一种颜色,因此不再有选择器(例如,在选择按钮时更改颜色)。

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

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