如何为活动切换主题 [英] How to Switch theme for an activity
问题描述
这是这种情况:
- 我有
DogActivity
和FavoritesActivity
.DogActivity
只是一个ListView
.当您单击列表中的Dog
时,它会将您带到FavoritesActivity
. - 我想准备一些主题.它们不需要动态生成.它们可以已经以XML形式存在.
- 要根据用户从列表中选择哪条狗,我希望在已有的主题之一中显示
FavoritesActivity
.
- I have
DogActivity
andFavoritesActivity
.DogActivity
is just aListView
. When you click on aDog
in the list, it takes you toFavoritesActivity
. - I want to have a number of themes ready to go. They don’t need to be dynamically generated. They can already exist in XML form.
- Depending on which dog the user selects from the list, I want to have the
FavoritesActivity
shown in one of my pre-existing themes.
我听过有关 ContextWrapper
的讨论,但是我不确定如何应用它.关于如何实现这一目标有什么想法吗?
I hear talks about ContextWrapper
, but I am not sure how to apply it. Any thoughts on how I may accomplish this?
详细信息:
这是通常的单个主题:
对于v21/styles.xml
<resources>
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
<item name="android:colorControlHighlight">@color/colorAccentLight</item>
<item name="android:colorControlNormal">@color/colorAccent</item>
<item name="android:itemTextAppearance">@style/AppTheme.itemTextStyle</item>
<item name="popupMenuStyle">@style/PopupMenu.MyAppTheme</item>
</style>
<style name="AppTheme.NoActionBar">
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
<item name="android:windowDrawsSystemBarBackgrounds">true</item>
<item name="android:statusBarColor">@android:color/transparent</item>
<item name="android:colorControlHighlight">@color/colorAccentLight</item>
</style>
<style name="AppTheme.itemTextStyle" parent="@android:style/TextAppearance.Widget.IconMenu.Item">
<item name="android:textColor">@color/colorPrimary</item>
</style>
</resources>
用于styles.xml
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
</style>
我想做的事
基本上,我只想即时更改 colorPrimary
, colorPrimaryDark
和 colorAccent
,并具有所有样式,主题和XML布局,用它们来改变.因此,如果我可以在启动 FavoritesActivity
之前更改这些颜色,那将解决我的问题.
Essentially I just want to change the colorPrimary
, colorPrimaryDark
and colorAccent
on the fly and have all the styles and themes and XML layouts that use them to change. So if I can change those colors before I launch FavoritesActivity
then that would solve my problems.
推荐答案
您可以只将狗类型作为Intent Extra发送,然后使用 setTheme()
方法设置适当的主题.
You can just send the dog type as an Intent extra, and then use the setTheme()
method to set the appropriate Theme.
对于此示例,假设您只有两个主题:
For this example, suppose you just have two Themes:
<style name="AppThemeOne" parent="Theme.AppCompat.Light.NoActionBar">
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
</style>
<style name="AppThemeTwo" parent="Theme.AppCompat.Light.NoActionBar">
<item name="colorPrimary">@color/colorPrimaryCustom</item>
<item name="colorPrimaryDark">@color/colorPrimaryDarkCustom</item>
<item name="colorAccent">@color/colorAccentCustom</item>
</style>
然后,在DogActivity中,将Intent Extra设置为用户从ListView中选择的Dog类型:
Then, in DogActivity, set an Intent Extra to the Dog type the user selected from the ListView:
Intent intent = new Intent(DogActivity.this, FavoritesActivity.class);
intent.putExtra("dog_type", "terrier");
startActivity(intent);
然后在收藏夹活动"中加载正确的主题:
Then, in FavoritesActivity, load the correct Theme:
@Override
protected void onCreate(Bundle savedInstanceState) {
String dogType = getIntent().getStringExtra("dog_type");
if (dogType.equals("terrier")) {
setTheme(R.style.AppThemeOne);
} else {
setTheme(R.style.AppThemeTwo);
}
super.onCreate(savedInstanceState);
setContentView(R.layout.favorites_layout);
//.....
}
这篇关于如何为活动切换主题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!