【问题标题】:How do I cascade a style by theme in android如何在android中按主题级联样式
【发布时间】: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


    【解决方案1】:

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

    Android 源代码中有三个用于此目的的文件:attrs.xmlstyles.xmlthemes.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 作为样式示例。

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

    【讨论】:

    • 啊!对我来说主要的关键是“style="?attr/sportLabelStyle"!:) THX!
    【解决方案2】:

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

    【讨论】:

      猜你喜欢
      • 2021-07-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-12-28
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多