【发布时间】:2011-09-20 22:40:39
【问题描述】:
基本上,我想要一个单一的布局,我可以在主题上以不同的方式skin。该网站上的许多示例和条目似乎都围绕着这个问题跳舞,所以我不完全确定它可以完成。或者我就是不明白。
这是概念。
假设我的应用与运动相关.. 该应用的默认值为“SportTheme”
我还希望用户说他们想要“足球”或“棒球”主题,并且在指定的 <TextView> 元素上,我希望文本(默认为“运动”)更改为“足球”或“棒球” ' 考虑到应用于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>
感谢您提供的任何见解
【问题讨论】:
标签: android text coding-style themes textview