簡體   English   中英

如何通過更改android中的主題來更改背景顏色?

[英]how to change background color by changing the theme in android?

我想在android中更改主題時更改背景顏色。 有沒有辦法像這樣設置XML背景

android:background="@styles/colorAccent"

因此,當我更改主題時,背景顏色會自動更改為相應主題的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>

這將應用於沒有手動設置背景的視圖。

最后,您可以通過以下方式應用主題

getTheme().applyStyle(R.style.YourTheme, true);

setContentView之前調用此方法

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM