简体   繁体   English

如何将两种不同的样式应用于android中的一个元素?

[英]How to apply two different styles to one element in android?

I have a TextView and I want to apply a Style which I use for all TextView elements plus another style which I only use within a specific Activity.我有一个 TextView,我想应用一个用于所有 TextView 元素的样式以及另一种仅在特定 Activity 中使用的样式。 Is there any possibility to do that?有没有可能这样做?

Just a little piece of information that might add to the overall value of the question - shamelessly copied from: http://developer.android.com/guide/topics/ui/themes.html#DefiningStyles只是一点点信息可能会增加问题的整体价值 - 无耻地复制自:http: //developer.android.com/guide/topics/ui/themes.html#DefiningStyles

If you want to inherit from styles that you've defined yourself, you do not have to use the parent attribute.如果您想继承您自己定义的样式,则不必使用 parent 属性。 Instead, just prefix the name of the style you want to inherit to the name of your new style, separated by a period.相反,只需将要继承的样式名称添加到新样式名称的前面,并用句点分隔。 For example, to create a new style that inherits the CodeFont style defined above, but make the color red, you can author the new style like this:例如,要创建一个继承上面定义的 CodeFont 样式的新样式,但将颜色设为红色,您可以像这样创作新样式:

  <style name="CodeFont.Red">
        <item name="android:textColor">#FF0000</item>
    </style>

Notice that there is no parent attribute in the tag, but because the name attribute begins with the CodeFont style name (which is a style that you have created), this style inherits all style properties from that style.请注意,标签中没有 parent 属性,但是由于 name 属性以 CodeFont 样式名称(这是您创建的样式)开头,因此该样式继承了该样式的所有样式属性。 This style then overrides the android:textColor property to make the text red.然后,此样式会覆盖 android:textColor 属性以使文本变为红色。 You can reference this new style as @style/CodeFont.Red.您可以将此新样式引用为@style/CodeFont.Red。

You can continue inheriting like this as many times as you'd like, by chaining names with periods.您可以通过将名称与句点链接起来,继续进行多次这样的继承。 For example, you can extend CodeFont.Red to be bigger, with:例如,您可以将 CodeFont.Red 扩展为更大,其中:

<style name="CodeFont.Red.Big">
    <item name="android:textSize">30sp</item>
</style>

A style under Android can have a parent style. Android 下的样式可以有父样式。

So just have MyActivityTextView define GeneralTextView as parent style, and change/add style properties.所以只需让MyActivityTextViewGeneralTextView定义为父样式,并更改/添加样式属性。

Then you can use MyActivityTextView for some views and GeneralTextView for the others.然后,您可以将MyActivityTextView用于某些视图,将GeneralTextView用于其他视图。

It's described in Defining Styles .它在定义样式中进行了描述。

You can extend one style with another in your style.xml:您可以在 style.xml 中用另一种样式扩展一种样式:

<style name="current_weekday_white" parent="current_day_white">
    <item name="android:textColor">#FFABAB</item>
</style>

Inherit one style from another and copy elements from third.从另一种风格继承一种风格,从第三种风格中复制元素。

<style name="Button.Style1" parent="android:style/Widget.Button">
    <item name="android:gravity">center</item>
    <item name="android:textSize">12sp</item>
    <item name="android:background">@drawable/shape_button</item>

    <!-- Here are attributes from third style -->
    <item name="android:fontFamily">sans-serif-medium</item>
    <item name="android:textColor">#112233</item>
</style>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM