压倒一切的文字颜色按键不灵 [英] Overriding text color for buttons not working

查看:218
本文介绍了压倒一切的文字颜色按键不灵的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经为每个按钮和buttonBarButtonStyle上的布局使用buttonBarStyle按钮栏自定义​​主题。
它工作正常,但我想更改按钮上的文本颜色,但它仍然需要默认的颜色(@android:彩色/ primary_text_holo_light)
我的code是:

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>

和的布局:

<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有父AppBaseTheme,其中有父母的android:Theme.Holo.Light.DarkActionBar

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

什么我必须做改变按钮的文本颜色?

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

编辑:
我试图将彩色每个元素和它的作品,但我想通过覆盖在样式文件中的颜色(保持所有的设计性能在同一个地方,类似CSS)来改变它。

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).

另外,我试图创建一个具有机器人的性能风格:ATTR / buttonBarStyle

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.

推荐答案

我想说,我不完全理解一个按钮栏是preface这个答案。 Android开发者文档似乎并没有对他们有任何提及,除了定义一些样式属性他们,所以我不确定他们是如何映射到实际视图/小工具。

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).

更多虽则点,属性等buttonBarStyle和buttonBarButtonStyle只能由活动用于解决默认样式按钮栏中的按钮栏和按钮。因为这些属性只能通过活动解决,如果直接应用到您的意见,他们将被忽略。

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"/>

请注意,这将适用于当前主题(其中,除非它是由一个活动覆盖,这将是整个应用程序相同)中定义的按钮栏样式。还要注意的是,因为你是引用了当前主题定义的样式,它允许你定义不同的当前主题取决于所需资源的预选赛,而无需改变你的布局code。

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.

这篇关于压倒一切的文字颜色按键不灵的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆