[英]The best way to manage android app themes
在我的程序中,我必須以編程方式切換應用程序主題。 也就是說,有一個選項可以切換淺色和深色主題。 最佳做法是什么? 我可以創建和管理樣式集嗎? 例如,我有這個textview和按鈕。
<Button
android:id="@+id/btn"
style="@style/BT_list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/OK" />
<TextView
android:id="@+id/tv"
style="@style/TText"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="@string/msg" />
我有這種風格:
<style name="BT_list">
<item name="android:layout_width">match_parent</item>
<item name="android:layout_height">30dp</item>
<item name="android:textColor">@color/green_color</item>
<item name="android:gravity">center</item>
<item name="android:paddingLeft">0dp</item>
<item name="android:paddingRight">0dp</item>
<item name="android:layout_marginLeft">0dp</item>
<item name="android:layout_marginRight">0dp</item>
<item name="android:textSize">15sp</item>
<item name="android:textStyle">bold</item>
<item name="android:background">@drawable/grad</item>
</style>
<style name="TText">
<item name="android:textColor">@color/text_color</item>
<item name="android:background">@color/white"</item>
</style>
如何使用setTheme();
類的值更改值setTheme();
以編程方式為這兩種(也許更多)樣式?
您可以創建一個首選項活動,使用戶可以選擇更改主題。 之后,在您要主題化的活動的OnCreate方法中,可以使用:
SharedPreferences prefs = PreferenceManager
.getDefaultSharedPreferences(this);
String userTheme = prefs.getString("theme", "1");
if (userTheme.equals("1"))
setTheme(R.style.ThemeDark);
else if (userTheme.equals("2"))
setTheme(R.style.ThemeLight);
在您的Styles.xml
您可以添加
<style name="ThemeDark" parent="Holo.Theme">
<!-- your changes go here -->
</style>
<style name="ThemeLight" parent="Holo.Theme.Light">
<!-- your changes go here -->
</style>
注意:這是我自己的與ABS和HoloEverywhere一起使用的主題更改方法。 如果您不使用這些庫,則將無法使用
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.