当我在android中改变主题时,我想改变背景颜色。有没有一种在XML中设置背景的方法,比如
android:background="@styles/colorAccent"
因此,当我更改主题时,背景颜色会自动更改为相应主题的colorAccent。任何替代方法也会有所帮助。
发布于 2019-07-09 09:19:20
样例布局
<FrameLayout
style="@style/colorAccentStyle"
android:layout_width="match_parent"
android:layout_height="wrap_content/>
styles.xml
<resources>
<style name="colorAccentStyle">
<item name="android:background">?colorAccent</item>
</style>
更简单的解决方案
<FrameLayout
android:background="?colorAccent"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
发布于 2019-07-09 09:43:43
使用此属性创建不同的主题
<style name="YourTheme" >
<item name="android:background">@color/yourcolor</item>
</style>
这将应用于没有手动设置背景的视图。
最后,您可以通过以下方式应用主题
getTheme().applyStyle(R.style.YourTheme, true);
在setContentView
之前调用此函数
https://stackoverflow.com/questions/56949059
复制相似问题