J W*_*ebb 1 android text themes coding-style textview
基本上,我想有一个单一的布局,我可以skin在主题上有所不同.这个网站上的许多示例和条目似乎都围绕这个问题跳了一下,所以我不完全确定它可以做到.或者我只是不明白.
这是概念.
假设我的应用程序与运动相关......应用程序默认为'SportTheme'我想用户也要说他们想要'足球'或'棒球'主题,并且在指定<TextView>元素上,我想要文本(默认为'Sport')更改为'Football'或'Baseball',因为整体主题适用于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>
感谢您提供的任何见解
要使用主题自定义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资源作为样式的示例.
如果问题不明确,请询问您的问题.
| 归档时间: | 
 | 
| 查看次数: | 2375 次 | 
| 最近记录: |