[英]Changing Android styles by using theme
I have an app with a few different Activities
. 我有一个带有一些不同
Activities
的应用程序。 The different activities have different styled buttons, texts, etc... I've set up all the components to have various styles based on their location/Activity. 不同的活动具有不同样式的按钮,文本等。我已经根据其位置/活动将所有组件设置为具有各种样式。 (Eg.
style="@style/MainMenuActionBarTitle
, or style="@style/MainMenuActionBarTagLine
). (例如
style="@style/MainMenuActionBarTitle
或style="@style/MainMenuActionBarTagLine
)。 These styles set the background
( Drawable
, MipMap
, or Color
), textColor
, etc... 这些样式设置
background
( Drawable
, MipMap
或Color
), textColor
等。
The app will be offering some theme packs which change the colors of these various components throughout the application, and I was hoping there was a way to have Styles
with the same name, but different values based on the Theme
of the app. 该应用程序将提供一些主题包,这些主题包会更改整个应用程序中这些各种组件的颜色,我希望有一种方法可以根据应用程序的
Theme
使用具有相同名称但不同值的Styles
。 This way I can just change the Theme
whenever the Activity
is loaded to whatever Theme the user has chosen. 这样,只要将
Activity
加载到用户选择的任何主题上,我就可以更改Theme
。
There's some good info here on how to change the standard widget look & feel using Themes, but that changes the look and feel for the standard-un-styled widgets. 有一些好的信息在这里就如何修改标准控件的外观和使用感觉的主题,但改变了外观和感觉为标准,未风格的小部件。
Is there a way to accomplish this using Themes, or is this the wrong direction altogether? 有没有一种方法可以使用主题完成此操作,或者这完全是错误的方向? Is there a better/easier route?
有更好/更便捷的路线吗?
Edit: After doing more research and more fiddling, I've realized what I want to do isn't far off from how I can accomplish this. 编辑:经过更多的研究和更多的摆弄之后,我意识到我想要做的事情与我如何实现这一目标并不遥远。 What I want to do is to actually change component
Styles
when I set the Theme
of the Activity
. 我要做的是在设置“
Activity
” Theme
时实际更改组件Styles
。
One solution I've discovered is to use attributes
which the Theme
can point to different Styles
. 我发现的一种解决方案是使用
Theme
可以指向不同Styles
attributes
。
attrs.xml attrs.xml
<resources>
<!-- Attributes referencing whatever style the theme needs to set up. -->
<attr name="main_menu_button_style_play" format="reference" />
</resources>
themes.xml 的themes.xml
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<!-- App specific attributes -->
<item name="@attr/main_menu_button_style_play">@style/MainMenu.Button.Play</item>
</style>
<!-- Blue application theme. -->
<style name="AppTheme.Blue" parent="AppTheme">
<!-- App specific attributes -->
<item name="@attr/main_menu_button_style_play">@style/MainMenu.Button.Play.Blue</item>
</style>
</resources>
styles.xml styles.xml
<style name="MainMenu.Button.Play">
<item name="android:background">#f76d3c</item>
<item name="android:text">PLAY</item>
</style>
<style name="MainMenu.Button.Play.Blue">
<item name="android:background">#2ca8c2</item>
</style>
activity.xml activity.xml
<Button android:id="@+id/main_play_button"
style="?attr/main_menu_button_style_play"/>
This works really well, and allows me to set the Theme
in the Activity.onCreate()
method. 这确实很好,并且允许我在
Activity.onCreate()
方法中设置Theme
。
The only annoying problem I have with this solution is that Android Studio complains that the Button
is missing the layout_width
and layout_height
even though they're defined in the Style
. 我对这种解决方案唯一的烦恼的问题是,Android Studio抱怨
Button
即使在Style
定义了Button
也缺少layout_width
和layout_height
。 I guess it doesn't follow the attribute reference back through the chosen Theme
. 我猜它没有通过所选的
Theme
跟随属性引用。
Another approach which is what I ended up using was to more heavily use the attributes. 我最终使用的另一种方法是更大量地使用属性。 Creating attributes for all the properties values I want to change between themes.
为我想要在主题之间更改的所有属性值创建属性。 So, instead of
main_menu_button_style_play
which defines the style reference, I used main_menu_button_play_background
. 所以,相反的
main_menu_button_style_play
它定义样式的引用,我用main_menu_button_play_background
。 This approach is the same amount of work as simply specifying a style because themes can inherit, but the IDE understands it. 这种方法与简单指定样式的工作量相同,因为主题可以继承,但是IDE可以理解。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.