【问题标题】:Android's ToggleButton - can't set textColor in style.xml fileAndroid 切换按钮 - 无法在 style.xml 文件中设置文本颜色
【发布时间】:2016-09-11 07:20:40
【问题描述】:

问题是 - 当我在样式中定义文本颜色时:

<style name="Widget.App.SearcherButton" parent="Widget.AppCompat.Button">
    <item name="android:textAllCaps">false</item>
    <item name="android:textColor">@color/buttontextcolor</item>
</style>

<ToggleButton
    android:layout_width="wrap_content"
    android:layout_height="@dimen/searcherButtonHeight"
    android:background="@drawable/button"
    android:theme="@style/Widget.App.SearcherButton"
    android:textOn="@string/favButtonText"
    android:textOff="@string/favButtonText" />

它不起作用 - 这意味着ToggleButton 将具有默认文本颜色(黑色)。 但是,如果我直接在 ToggleButton 上设置 textColor 属性:

<ToggleButton
    android:layout_width="wrap_content"
    android:layout_height="@dimen/searcherButtonHeight"
    android:background="@drawable/button"
    android:textColor="@color/buttontextcolor"
    android:theme="@style/Widget.App.SearcherButton"
    android:textOn="@string/favButtonText"
    android:textOff="@string/favButtonText" />

然后就可以了。为什么会这样以及如何在样式中声明时使其工作?

【问题讨论】:

标签: android


【解决方案1】:

您需要将样式添加到 ToggleButton 本身,而不仅仅是使用 'android:theme=" " '。类似的东西

<ToggleButton
android:id = "@+id/toggleButton"
...
style = "@style/stylename"/>

【讨论】:

  • 根据 developer.android.com 的样式是“样式是指定 视图或窗口的外观和格式的属性的集合。”。 developer.android.com 将主题定义为“应用于整个 Activity 或应用程序,而不是单个 View 的样式”。您可以查看此link 了解更多信息。请告诉我是否你不明白我的比喻,所以我可以寻找另一个。干杯。
  • 这个解释可以。谢谢!
猜你喜欢
  • 2015-05-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-12-07
  • 1970-01-01
  • 1970-01-01
  • 2020-07-16
  • 2016-08-29
相关资源
最近更新 更多