【问题标题】:Android android:buttonStyle not appliedAndroid android:buttonStyle 未应用
【发布时间】:2015-04-27 15:11:35
【问题描述】:

我的 styles.xml 中有以下代码:

<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <item name="android:buttonStyle">@style/ButtonStyle</item>
</style>

<style name="ButtonStyle" parent="@android:style/Widget.Button">
    <item name="android:background">#FF00FF</item>
    <item name="android:textColor">@color/material_deep_teal_500</item>
</style>

这应该以粉红色 (#FF00FF) 打印按钮背景并以蓝绿色打印文本,但它不适用于我的按钮。

我有两种视图,普通 Button() 和 ButtonLinearLayout,它们获取当前它们的默认 Button backgroundm,并将其应用到 LinearLayout。

但是当我尝试将它应用到我的视图/默认按钮时它没有改变。

【问题讨论】:

  • 嗯,我检查了,我可以在布局编辑器上看到背景和 textColor 的颜色,但在设备中看不到:S

标签: android button styles


【解决方案1】:

最简单的方法是创建一个res/drawable/button_selector.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

<item android:state_pressed="true"
      android:color="@color/material_deep_teal_500" >  //text color when button is pressed
    <shape>
        <solid
            android:color="#ff00ff"/>  //background color when button is pressed
    </shape>
</item>
<item android:color="@color/material_deep_teal_500"> //text color when button is normal
    <shape>
        <solid android:color="#ff00ff"/> //background color when button is normal
    </shape>
</item>

</selector>

现在将android:background="@drawable/button_selector" 放入具有按钮的xml

【讨论】:

  • 感谢您的选择。但这没有任何区别,我可以在布局编辑器中看到它,但在设备中看不到:$ 如果有帮助,我正在使用带有 NoTitle 的 DialogFragment
  • 您不能以编程方式将此背景设置为对话框片段按钮吗? @alorma
  • 我想我可以,但它更复杂,因为我正在使用这个 buttonStyle 来检索按钮背景可绘制并应用于其他类型的视图
  • @alorma 尝试将parent="@android:style/Widget.Button" 更改为parent="@android:style/Widget.AppCompat.Light.Button,因为Widget.Button" 没有定义父主题
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-02-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多