如何在android中通过改变主题来改变背景色?
我想在android中更改主题时更改背景色。有没有一种方法可以像XML一样设置背景如何在android中通过改变主题来改变背景色?,android,xml,android-layout,android-theme,Android,Xml,Android Layout,Android Theme,我想在android中更改主题时更改背景色。有没有一种方法可以像XML一样设置背景 android:background="@styles/colorAccent" 因此,当我更改主题时,背景颜色会自动更改为相应主题的颜色重音。任何替代方法也会有所帮助。示例布局 <FrameLayout style="@style/colorAccentStyle" android:layout_width="match_parent" android:layout_heigh
android:background="@styles/colorAccent"
因此,当我更改主题时,背景颜色会自动更改为相应主题的颜色重音。任何替代方法也会有所帮助。示例布局
<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"/>
使用此属性创建不同的主题
<style name="YourTheme" >
<item name="android:background">@color/yourcolor</item>
</style>
在setContentView之前调用此选项您应该根据所选主题手动更改背景,并且您需要手动更改背景,这没有捷径可走。实际上,我想通过单击按钮为应用程序实现不同的主题,我想更改主题,这样,如果我采用此方法,所有颜色都会自动更改,我必须为每个视图添加检查,这不是可行的方法。你应该为它创建多个主题,并在每个主题内分别添加背景属性,这样只有你才能实现。请给出一个带有小代码段的简短示例,我将能够更好地理解。
getTheme().applyStyle(R.style.YourTheme, true);