覆盖不起作用的按钮的文本颜色

And*_*i F 5 android colors textcolor android-theme android-button

我已经在每个按钮上使用 buttonBarStyle 和在布局上使用 buttonBarButtonStyle 为按钮栏创建了一个自定义主题。它工作正常,但我想更改按钮的文本颜色,但它仍然采用默认颜色(@android:color/primary_text_holo_light)我的代码是:

<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>
Run Code Online (Sandbox Code Playgroud)

对于布局:

<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>
Run Code Online (Sandbox Code Playgroud)

AppTheme 有父 AppBaseTheme,它有父 android:Theme.Holo.Light.DarkActionBar。

我该怎么做才能更改按钮的文本颜色?

编辑: 我尝试在每个元素上应用颜色并且它有效,但我想通过覆盖样式文件中的颜色来更改它(将所有设计属性保留在一个地方,类似于 CSS)。

另外,我试图创建一个具有 ?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>
Run Code Online (Sandbox Code Playgroud)

但它的行为很奇怪:颜色已更新,但父样式的属性不可见。

The*_*eIT 4

我想在这个答案的开头说我不完全理解按钮栏是什么。除了为它们定义一些样式属性之外,Android开发人员文档似乎没有提及它们,因此我不确定它们如何映射到实际的视图/小部件。

我相信发生的事情是您忽略了风格和主题之间的区别。样式应用于视图以定义其属性。主题应用于整个应用程序或单个活动,并提供为应用该主题的应用程序/活动中显示的视图定义默认样式的能力(除其他外)。

但更重要的是,Activity 仅使用诸如buttonBarStyle 和buttonBarButtonStyle 之类的属性来解析按钮栏和按钮栏中按钮的默认样式。因为这些属性仅由活动解析,所以如果直接应用于视图,它们将被忽略。

因此,如果您想使用主题来应用样式,则需要创建一个新的样式定义来扩展所需的基本样式,然后将该自定义样式分配回主题定义中相应的视图样式属性。

<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" >
Run Code Online (Sandbox Code Playgroud)

所有没有显式样式属性的按钮现在都将显示为白色文本。

但是,对于想要显示具有 ButtonBar 样式的按钮的特殊情况,您应该注意 ButtonBar 样式不会自动应用于您的按钮。您必须手动定义要与该样式一起显示的按钮。因此,上面的示例现在看起来更像这样:

<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" >
Run Code Online (Sandbox Code Playgroud)

现在应用您为当前主题定义的 ButtonBar 样式:

<Button
        ...
        style="?android:attr/buttonBarStyle"/>
Run Code Online (Sandbox Code Playgroud)

请注意,这将应用为当前主题定义的 ButtonBar 样式(除非它被 Activity 覆盖,否则它将在整个应用程序中保持不变)。另请注意,由于您引用为当前主题定义的样式,因此它允许您根据所需的资源限定符以不同的方式定义当前主题,而无需更改布局代码。