如何在android中按主题级联样式
基本上,我想要一个单一的布局,我可以在主题上以不同的方式皮肤
。该网站上的许多示例和条目似乎都围绕着这个问题,所以我不完全确定它可以完成。或者我只是不明白。
这是这个概念。
假设我的应用程序与运动相关......该应用程序的默认主题为“SportTheme” 我还希望用户说他们想要“足球”或“棒球”主题,并且在指定的
元素上,我希望文本(默认为“体育”)鉴于应用于活动
的整体主题,更改为“足球”或“棒球”?
在activityA.java 中的strings.xml 中
<string name="label_sport">Sport</string>
<string name="label_football">Football</string>
<string name="label_baseball">Baseball</string>
- 这里重要的是为活动设置主题(或者应用程序也可以)。
@Override
protected void onCreate(Bundle savedInstanceState)
{
Log.d(TAG, "onCreate");
super.onCreate(savedInstanceState);
this.setContentView(R.layout.layout_a);
switch (user.ThemePreference)
{
case FOOTBALL_THEME:
this.setTheme(R.style.FootballTheme);
break;
case BASEBALL_THEME:
this.setTheme(R.style.BaseballTheme);
break;
default:
this.setTheme(R.style.SportTheme);
break;
}
}
在layout_a.xml 的
...
<TextView
android:id="@+id/tvSport"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:text="@string/label_sport"
android:style="@style/SportLabel"></TextView>
主题/样式中我该做什么?像这样的东西吗?这里重要的是 TextView 中的文本。我将在整个应用程序的几个不同活动中使用相同的 textView。
<theme name="SportTheme" parent="android:Theme" />
<theme name="FootballTheme" parent="SportTheme">
<item name="android:background">@color/brown</item>
</theme>
<theme name="BaseballTheme" parent="SportTheme">
<item name="android:background">@color/green</item>
</theme>
<theme name="SportTheme.SportLabel">
<item name="android:text">@string/label_sport</item>
</theme>
<theme name="FootballTheme.SportLabel">
<item name="android:text">@string/label_football</item>
<item name="android:textColor">@color/black</item>
</theme>
<theme name="BaseBallTheme.SportLabel">
<item name="android:text">@string/label_baseball</item>
<item name="android:textColor">@color/white</item>
</theme>
感谢您提供的任何见解
Basically, I'd like to have a single layout that I can skin
differently on the theme. Many examples and entries on this site seem dance around the issue a little so I'm not entirely certain it can be done. Or I just don't get it.
Here's the concept.
Let's say my app is sports-related.. the app has a default them of 'SportTheme'
I'd like users also to say they want the 'Football' or 'Baseball' theme, and on designated <TextView>
elements, I'd like the text (defaults to 'Sport') to change to 'Football' or 'Baseball' given the overall theme applied to the activity
?
in strings.xml
<string name="label_sport">Sport</string>
<string name="label_football">Football</string>
<string name="label_baseball">Baseball</string>
in activityA.java - The important thing here is that the theme is set for the activity (or application is fine, too).
@Override
protected void onCreate(Bundle savedInstanceState)
{
Log.d(TAG, "onCreate");
super.onCreate(savedInstanceState);
this.setContentView(R.layout.layout_a);
switch (user.ThemePreference)
{
case FOOTBALL_THEME:
this.setTheme(R.style.FootballTheme);
break;
case BASEBALL_THEME:
this.setTheme(R.style.BaseballTheme);
break;
default:
this.setTheme(R.style.SportTheme);
break;
}
}
in layout_a.xml
...
<TextView
android:id="@+id/tvSport"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:text="@string/label_sport"
android:style="@style/SportLabel"></TextView>
What do I do in themes/styles? Something like this? The important thing here is the text in the TextView. I'll be using the same textView in several different activities throughout the application.
<theme name="SportTheme" parent="android:Theme" />
<theme name="FootballTheme" parent="SportTheme">
<item name="android:background">@color/brown</item>
</theme>
<theme name="BaseballTheme" parent="SportTheme">
<item name="android:background">@color/green</item>
</theme>
<theme name="SportTheme.SportLabel">
<item name="android:text">@string/label_sport</item>
</theme>
<theme name="FootballTheme.SportLabel">
<item name="android:text">@string/label_football</item>
<item name="android:textColor">@color/black</item>
</theme>
<theme name="BaseBallTheme.SportLabel">
<item name="android:text">@string/label_baseball</item>
<item name="android:textColor">@color/white</item>
</theme>
Thanks for any insight you can provide
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
要使用主题自定义 UI,您需要定义要在主题内自定义的属性,并在布局中使用对这些属性的引用(例如
attr/backgroundColor
)。Android 源代码中有三个文件用于此目的:attrs.xml、styles.xml 和 themes.xml。如果您需要一些自定义属性进行自定义,那么您应该在 attrs.xml 中声明它们。如果您打算仅使用预定义的 Android 属性,则无需创建此文件。
styles.xml 文件用于定义属性值集。例如,您可以为每个小部件定义不同的样式集。
themes.xml 是用于自定义的主文件。所有主题通常都在此文件中定义。您可以通过多种方式自定义某些内容。例如,您可以在主题中定义默认值并从布局中引用它。您还可以定义对样式的引用。
layout.xml
请注意,样式的使用没有
android
命名空间。这不是一个错字。因此,如果您想使用主题自定义布局,您可以创建多个主题并定义属性和属性集(样式)的默认值,并使用
听起来很困难但事实并非如此引用这些值。您可以使用 Android 资源 作为样式示例。
如果有不清楚的地方,请提出您的问题。
To customize your UI with themes you need to define attributes you want to customize inside your themes and use references to these attributes in layouts (e.g.
attr/backgroundColor
).There're three files in Android sources which are used for this purpose: attrs.xml, styles.xml and themes.xml. If you need some custom attributes for customization then you should declare them in attrs.xml. If you're going to use only predefined Android attributes then you don't need to create this file.
The styles.xml file is used for defining sets of attribute values. For example you can define different style sets for each widget.
The themes.xml is the main file used for customizing. All themes are usually defined in this file. You can customize something in several ways. For example you can define a default value in the theme and reference it from a layout. Also you can define a reference to a style.
layout.xml
Notice that style is used without the
android
namespace. That's not a typo.So if you want to customize your layout using themes you can create several themes and define default values for attributes and attribute sets (styles) and reference these values using
Sounds difficult but it's not really. You can use Android resources as an example of styling.
Please ask your question if something is not clear.
我写了一篇关于 主题 的博客文章,可能会对您有所帮助。还有一系列关于自定义控件的文章,解释了如何创建可主题化的自定义控件,这提供了有关 Android 主题如何工作的更多信息。
I have written a blog post about Themes which may help you. There is also a series of article on Custom Controls which explains how to create a custom control which is themeable, and this provides additional information about how Android themes work.