基本上,我想要一个单一的布局,我可以skin
在主题上有所不同。这个网站上的许多示例和条目似乎都围绕这个问题跳了一下,所以我不完全确定它可以做到。或者我只是不明白。
这是概念。
假设我的应用程序与运动有关..应用程序默认为'SportTheme'
我希望用户也说他们想要'足球'或'棒球'主题,并且在指定的<TextView>
元素上,我希望文本(默认为'Sport')更改为'Football'或'棒球'给出了应用于activity
的整体主题?
在strings.xml中
<string name="label_sport">Sport</string>
<string name="label_football">Football</string>
<string name="label_baseball">Baseball</string>
activityA.java中的- 这里重要的是为活动设置主题(或者应用程序也很好)。
@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>
感谢您提供的任何见解
答案 0 :(得分:6)
要使用主题自定义UI,您需要在主题中定义要自定义的属性,并在布局中使用对这些属性的引用(例如attr/backgroundColor
)。
Android源中有三个用于此目的的文件: attrs.xml , styles.xml 和 themes.xml 。如果您需要一些自定义属性进行自定义,则应在 attrs.xml 中声明它们。如果您只使用预定义的Android属性,则无需创建此文件。
<declare-styleable name="SportTheme">
<attr name="customAttribute" format="color" />
<attr name="sportLabelStyle" format="reference" />
</declare-styleable>
styles.xml 文件用于定义属性值集。例如,您可以为每个小部件定义不同的样式集。
<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>
themes.xml 是用于自定义的主文件。所有主题通常都在此文件中定义。您可以通过多种方式自定义内容。例如,您可以在主题中定义默认值并从布局中引用它。您还可以定义对样式的引用。
<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>
<强> layout.xml 强>
<TextView
android:background="?android:attr/colorBackground"
android:textColor="?attr/customAttribute"
style="?attr/sportLabelStyle" />
请注意,在没有android
命名空间的情况下使用样式。这不是一个错字。
因此,如果您想使用主题自定义布局,可以创建多个主题并定义属性和属性集(样式)的默认值,并使用
引用这些值?[android:]attr/attributeName
听起来很难,但事实并非如此。您可以使用Android resources作为样式的示例。
如果不清楚,请询问您的问题。
答案 1 :(得分:0)
我撰写了一篇关于Themes的博文,可能会对您有所帮助。还有一系列关于Custom Controls的文章解释了如何创建一个可定制的自定义控件,这提供了有关Android主题如何工作的其他信息。