如何在android中按主题级联样式

如何在android中按主题级联样式,android,text,coding-style,themes,textview,Android,Text,Coding Style,Themes,Textview,基本上,我想有一个单一的布局,我可以皮肤不同的主题。这个网站上的许多例子和条目似乎围绕着这个问题跳了一圈,所以我不能完全肯定它能做到。或者我就是不明白 这是一个概念 假设我的应用程序与体育相关。。应用程序的默认名称为“SportTheme” 我希望用户也说他们想要“足球”或“棒球”主题,并且在指定的元素上,我希望文本(默认为“运动”)更改为“足球”或“棒球”,因为活动应用了整个主题 在strings.xml中 <string name="label_sport">Sport</

基本上,我想有一个单一的布局,我可以
皮肤
不同的主题。这个网站上的许多例子和条目似乎围绕着这个问题跳了一圈,所以我不能完全肯定它能做到。或者我就是不明白

这是一个概念

假设我的应用程序与体育相关。。应用程序的默认名称为“SportTheme” 我希望用户也说他们想要“足球”或“棒球”主题,并且在指定的
元素上,我希望文本(默认为“运动”)更改为“足球”或“棒球”,因为
活动应用了整个主题

在strings.xml中

<string name="label_sport">Sport</string>
<string name="label_football">Football</string>
<string name="label_baseball">Baseball</string>
在layou_.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

<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>

@颜色/棕色
@颜色/绿色
@字符串/标签\u运动
@字符串/标签\u足球
@颜色/黑色
@字符串/标签
@颜色/白色

感谢您提供的任何见解

我写了一篇博客文章,可能会对您有所帮助。还有一系列文章解释了如何创建可主题化的自定义控件,并提供了有关Android主题如何工作的其他信息。

我写了一篇博客文章,可能会对您有所帮助。还有一系列文章解释了如何创建可主题化的自定义控件,并提供了有关Android主题如何工作的其他信息。

要使用主题自定义UI,您需要在主题内定义要自定义的属性,并在布局中使用对这些属性的引用(例如,
attr/backgroundColor

Android源代码中有三个用于此目的的文件:attrs.xmlstyle.xmlthemes.xml。如果您需要一些自定义属性进行自定义,那么您应该在attrs.xml中声明它们。如果您只使用预定义的Android属性,那么就不需要不需要创建此文件

<declare-styleable name="SportTheme">
    <attr name="customAttribute" format="color" />
    <attr name="sportLabelStyle" format="reference" />
</declare-styleable>
听起来很难,但其实不是。你可以用它作为一个造型的例子


如果有不清楚的地方,请询问您的问题。

要使用主题自定义UI,您需要在主题中定义要自定义的属性,并在布局中使用对这些属性的引用(例如
attr/backgroundColor

Android源代码中有三个用于此目的的文件:attrs.xmlstyle.xmlthemes.xml。如果您需要一些自定义属性进行自定义,那么您应该在attrs.xml中声明它们。如果您只使用预定义的Android属性,那么就不需要不需要创建此文件

<declare-styleable name="SportTheme">
    <attr name="customAttribute" format="color" />
    <attr name="sportLabelStyle" format="reference" />
</declare-styleable>
听起来很难,但其实不是。你可以用它作为一个造型的例子

如果有不清楚的地方,请提问。

Ah!我的主键是“style=“?attr/sportLabelStyle!”:)THX!Ah!我的主键是“style=“?attr/sportLabelStyle!”:)谢谢!
<style name="Widget.TextView.SportLabel" parent="@android:style/Widget.TextView">
    <item name="android:textColor">@android:color/white</item>
    <item name="android:textSize">20sp</item>
</style>
<style name="Theme.FootballTheme" parent="@android:style/Theme">
    <!-- define value for predefined Android attribute -->
    <item name="android:colorBackground">@android:color/white</item>
    <!-- define value for custom attribute -->
    <item name="customAttribute">@android:color/black</item>
    <!-- define reference to a style -->
    <item name="sportLabelStyle">@style/Widget.TextView.SportLabel</item>
</style>
<TextView
    android:background="?android:attr/colorBackground"
    android:textColor="?attr/customAttribute"
    style="?attr/sportLabelStyle" />
?[android:]attr/attributeName