Overriding text color for buttons not working

有些话、适合烂在心里 提交于 2019-12-10 16:28:54

问题


I've created a custom theme for a button bar using buttonBarStyle on each button and buttonBarButtonStyle on the layout. It works fine, but i want to change the text color for the buttons, but it still takes the default color (@android:color/primary_text_holo_light) My code is:

<style name="ButtonBarTheme" parent="AppTheme">
        <item name="buttonBarStyle">?android:attr/buttonBarStyle</item>
        <item name="buttonBarButtonStyle">?android:attr/buttonBarButtonStyle</item>
        <item name="android:textColor">#ffffff</item>
</style>

And for the layout:

<LinearLayout 
    style="?android:attr/buttonBarStyle"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="#0090cc"
    android:orientation="horizontal" >

    <Button
        android:id="@+id/bar_button1"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        style="@style/ButtonBarStyleButton"
        android:text="Button 1" />

    <Button
        android:id="@+id/bar_button2"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        style="?android:attr/buttonBarButtonStyle"
        android:text="Button 2" />

</LinearLayout>

AppTheme has parent AppBaseTheme, which has parent android:Theme.Holo.Light.DarkActionBar.

What do I have to do to change the buttons' text color?

EDIT: I tried applying the color on each element and it works, but I want to change it by overriding the color in the style file (keep all design properties in one place, similar to CSS).

Also, I have tried to create a style that has the properties of ?android:attr/buttonBarStyle"

   <style name="ButtonBarStyleButtonBase">
        <item name="buttonBarButtonStyle">?android:attr/buttonBarButtonStyle</item>
    </style>

   <style name="ButtonBarStyleButton" parent="ButtonBarStyleButtonBase">
        <item name="android:textColor">#ffFFff</item>
    </style>

but it behaves strangely: the color is updated, but no properties form the parent style are visible.


回答1:


I'd like to preface this answer by saying that I don't fully understand what a button bar is. The android developer docs don't seem to have any mention of them, besides defining some style attributes for them, therefore I'm uncertain as to how they map to actual Views/Widgets.

I believe what's going on is that you're missing the distinction between a Style and a Theme. A Style is applied to a View to define it's attributes. A Theme is applied to either the entire Application or an individual Activity and provides the ability to define default styles for the Views displayed within the Application/Activity on which the Theme is applied (among other things).

More to the point though, attributes such as buttonBarStyle and buttonBarButtonStyle will only be used by the Activity to resolve default styles for the button bars and Buttons within button bars. Because these attributes are only resolved by the Activity, they will be ignored if applied directly to your Views.

Therefore if you want to use Themes to apply your Styles, you'll need to create a new Style definition which extends the desired base Style, then assign that custom Style back to the appropriate View Style attribute in your Theme definition.

<style name="CustomButtonStyle" parent="@android:attr/buttonStyle">
        <item name="android:textColor">#ffffff</item>
</style>


<style name="AppTheme" parent="@android:style/Theme.Holo.Light.DarkActionBar">
        <item name="buttonStyle">@style/CustomButtonStyle</item>
</style>


<application
    ...
    android:theme="@style/AppTheme" >

All Buttons without an explicit style attribute will now appear with white text.

However, for your special case of wanting to display Buttons with a ButtonBar Style, you should note that the ButtonBar Style isn't automatically applied to your Buttons. You have to manually define the Buttons that you want to appear with that Style. As such, the example above will now look more like this:

<style name="CustomButtonBarStyle" parent="@android:attr/buttonBarStyle">
        <item name="android:textColor">#ffffff</item>
</style>


<style name="AppTheme" parent="@android:style/Theme.Holo.Light.DarkActionBar">
        <item name="buttonBarStyle">@style/CustomButtonBarStyle</item>
</style>


<application
    ...
    android:theme="@style/AppTheme" >

Now apply the the ButtonBar style you have defined for the current Theme:

<Button
        ...
        style="?android:attr/buttonBarStyle"/>

Note that this will apply the ButtonBar style defined for the current Theme (which unless it is overridden by an Activity, it will be the same throughout your application). Also note that because you are referencing the Style defined for the current Theme, it allows you to define the current theme differently depending on desired resource qualifiers, without having to change your layout code.




回答2:


I tried the other solution given here, with no luck. Attempting to use a private style (android:attr/buttonBarStyle, in this case) as a parent gives errors.

A solution to the problem of changing the color of buttons under buttonBarStyle is to recreate android:attr/buttonBarStyle for yourself by assigning your buttons a background:

android:background="?android:attr/selectableItemBackground"

and then specifying:

android:textColor="@color/your_color"

which will correctly change the text color of the buttons while preserving buttonBarButtonStyle's appearance.



来源:https://stackoverflow.com/questions/21538548/overriding-text-color-for-buttons-not-working

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!