[英]How to have multiple theme in my android app?
This is the home screen of my app.这是我的应用程序的主屏幕。 I have used
Drawable
and Color
resources in the design.我在设计中使用了
Drawable
和Color
资源。 Now I want to have multiple color sets (for the parts with cyan color) so user can change them at run time.现在我想要多个颜色集(对于青色的部分),以便用户可以在运行时更改它们。 I searched a lot, but they don't fit my case.
我搜索了很多,但它们不适合我的情况。
Note that I want just the cyan colored parts to change.请注意,我只想改变青色部分。 I know I can change all the
TextView
styles using style
attribute, but I just need certain views get certain drawables or colors as background.我知道我可以使用
style
属性更改所有TextView
styles ,但我只需要某些视图获取某些可绘制对象或 colors 作为背景。
I found a solution here but I cannot use it because it needs the min sdk higher than 21 while mine is 16.我在这里找到了一个解决方案,但我不能使用它,因为它需要最小 sdk 高于 21,而我的是 16。
You can easily set theme like this:您可以像这样轻松设置主题:
public void onCreate(Bundle savedInstanceState) {
setTheme(R.style.your_theme);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity)
}
Define your style in values/styles.xml
like so:在
values/styles.xml
中定义您的样式,如下所示:
<style name="AppTheme.MyTheme" parent="AppTheme.NoActionBar">
<item name="colorPrimary">@color/primaryColorCyan</item>
<item name="colorPrimaryDark">@color/primaryDarkColorCyan</item>
<item name="colorAccent">@color/secondaryColorCyan</item>
</style>
And then call setTheme(R.style.MyTheme)
before setContentView()
in onCreate()
method.然后在
onCreate()
方法中的setContentView()
) 之前调用setTheme(R.style.MyTheme)
。 setTheme
introduced in API LEVEL 1
. setTheme
在API LEVEL 1
中引入。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.