我想在主题中设置所有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>
唯一的区别是缺少&#39; android:&#39;在&#39; imageButtonStyle&#39;前面的前缀属性。
所以我的问题是:
有趣的是,它似乎是&#39; android:imageButtonStyle&#39;版本在几年前已经有效:How to apply an style to all ImageButtons in Android?。尽管如此,我还没有对自己进行过测试。
这是建议删除android前缀的帖子。包括未答复的评论,询问其工作原理:buttonStyle not working for 22.1.1
答案 0 :(得分:0)
您使用的android标记用于来自Android SDK的属性。
如果您使用支持库,则使用app标记.app只是自定义视图的任何自定义参数的命名空间。
这可以是任何东西,但是如果你看到那里的根元素可能是一行xmlns:app =&#34; http://schemas.android.com/apk/res-auto"分配命名空间
如果您使用自定义视图(自己的视图或构成库),您可能还会看到其他命名空间。
答案 1 :(得分:0)
如果其他人偶然发现了问题:我在Droidcon的演讲中找到了答案:https://youtu.be/Jr8hJdVGHAk?t=21m12s
从21:12开始,主题将在一分钟内完成。
据我所知,指定没有命名空间会导致被搜索的全局命名空间出现,这似乎包括支持库属性。事实上,SDK's R.attr和support library's R.attr都定义了imageButtonStyle
属性(略有不同的描述)。但是,支持库未定义textViewStyle
属性。这就解释了为什么你不能省略android:
前缀。
回答我关于文档的最后一个问题:尽管谷歌指南和R.attr课程&#39;文档,上面提到的视频和Google I / O谈话都非常有用:https://www.youtube.com/watch?v=TIHXGwRTMWI
因此唯一的问题是SDK imageButtonStyle
无效的原因。