【发布时间】:2017-09-12 13:41:18
【问题描述】:
我想在一个主题中设置我所有的 ImageButtons 样式。经过一段时间的搜索,我找到了解决问题的方法。但我不知道为什么会这样。
我的主要布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!" />
<ImageButton
android:id="@+id/imageButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:srcCompat="@mipmap/ic_launcher_foreground" />
</LinearLayout>
这是我原来的不起作用的主题。它设置了我的 TextView 的样式,但忽略了 ImageButton。结果如下面的截图所示。
<resources>
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="android:imageButtonStyle">@style/redBackground</item>
<item name="android:textViewStyle">@style/redBackground</item>
</style>
<style name="redBackground">
<item name="android:background">#FF0000</item>
</style>
</resources>
这是有效的主题:
<resources>
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="imageButtonStyle">@style/redBackground</item>
<item name="android:textViewStyle">@style/redBackground</item>
</style>
<style name="redBackground">
<item name="android:background">#FF0000</item>
</style>
</resources>
唯一的区别是“imageButtonStyle”属性前面缺少“android:”前缀。
所以我的问题是:
- imageButtonStyle 和 android:imageButtonStyle 有什么区别?
- 为什么 android:textViewStyle 有效但 android:imageButtonStyle 无效?它们都被定义为 'attrs.xml' 平台。
- 为什么没有textViewStyle(没有android前缀)?删除前缀会产生错误。
- 没有前缀的属性定义在哪里?显然不在平台“attrs.xml”中。
- 我在哪里可以找到适合整个风格的文档?当然,我已经阅读了相应的 Google 文档 (https://developer.android.com/guide/topics/ui/look-and-feel/themes.html)。但我仍然有类似这样的基本问题。
有趣的是,'android:imageButtonStyle' 版本似乎在几年前就可以使用:How to apply an style to all ImageButtons in Android?。不过,我自己还没有测试过。
这是建议删除 android 前缀的帖子。包括询问它为什么起作用的未回答的 cmets:buttonStyle not working for 22.1.1
【问题讨论】:
标签: android