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

发布于 2024-11-16 15:56:54 字数 2350 浏览 2 评论 0原文

基本上,我想要一个单一的布局,我可以在主题上以不同的方式皮肤。该网站上的许多示例和条目似乎都围绕着这个问题,所以我不完全确定它可以完成。或者我只是不明白。

这是这个概念。

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

在activityA.java 中的strings.xml 中

<string name="label_sport">Sport</string>
<string name="label_football">Football</string>
<string name="label_baseball">Baseball</string>

- 这里重要的是为活动设置主题(或者应用程序也可以)。

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

感谢您提供的任何见解

Basically, I'd like to have a single layout that I can skin differently on the theme. Many examples and entries on this site seem dance around the issue a little so I'm not entirely certain it can be done. Or I just don't get it.

Here's the concept.

Let's say my app is sports-related.. the app has a default them of 'SportTheme'
I'd like users also to say they want the 'Football' or 'Baseball' theme, and on designated <TextView> elements, I'd like the text (defaults to 'Sport') to change to 'Football' or 'Baseball' given the overall theme applied to the activity?

in strings.xml

<string name="label_sport">Sport</string>
<string name="label_football">Football</string>
<string name="label_baseball">Baseball</string>

in activityA.java - The important thing here is that the theme is set for the activity (or application is fine, too).

@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;
    }
}

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

What do I do in themes/styles? Something like this? The important thing here is the text in the TextView. I'll be using the same textView in several different activities throughout the application.

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

Thanks for any insight you can provide

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

提赋 2024-11-23 15:56:54

要使用主题自定义 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 资源 作为样式示例。

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

To customize your UI with themes you need to define attributes you want to customize inside your themes and use references to these attributes in layouts (e.g. attr/backgroundColor).

There're three files in Android sources which are used for this purpose: attrs.xml, styles.xml and themes.xml. If you need some custom attributes for customization then you should declare them in attrs.xml. If you're going to use only predefined Android attributes then you don't need to create this file.

<declare-styleable name="SportTheme">
    <attr name="customAttribute" format="color" />
    <attr name="sportLabelStyle" format="reference" />
</declare-styleable>

The styles.xml file is used for defining sets of attribute values. For example you can define different style sets for each widget.

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

The themes.xml is the main file used for customizing. All themes are usually defined in this file. You can customize something in several ways. For example you can define a default value in the theme and reference it from a layout. Also you can define a reference to a 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>

layout.xml

<TextView
    android:background="?android:attr/colorBackground"
    android:textColor="?attr/customAttribute"
    style="?attr/sportLabelStyle" />

Notice that style is used without the android namespace. That's not a typo.

So if you want to customize your layout using themes you can create several themes and define default values for attributes and attribute sets (styles) and reference these values using

?[android:]attr/attributeName

Sounds difficult but it's not really. You can use Android resources as an example of styling.

Please ask your question if something is not clear.

思念满溢 2024-11-23 15:56:54

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

I have written a blog post about Themes which may help you. There is also a series of article on Custom Controls which explains how to create a custom control which is themeable, and this provides additional information about how Android themes work.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文