wutzebaer
wutzebaer

Reputation: 14875

XML @style in parent

In the android examples style-parents are defined like this

 <style name="GreenText" parent="@android:style/TextAppearance">

but in the android sources i find

<style name="Widget.AppCompat.ActionBar.TabText" parent="Base.Widget.AppCompat.ActionBar.TabText">

whats the difference when i prefix with @style and @android:style or not?

Upvotes: 6

Views: 614

Answers (2)

DDsix
DDsix

Reputation: 1985

In general, if you put "@android" in front of something, it means that you're looking for a resource defined in android package, not in your project.

For instance, if you're trying to get a color:

android:background="@android:color/holo_red_dark"

This will get the Android holo_red_dark color. You don't have this color defined in your project.

android:background="@color/my_red_color"

This will get your "my_red_color" defined in your project.

Same goes for styles.

EDIT: The thing is there is no difference between

parent="@style/MyStyle"

and

parent="MyStyle"

for a style compiled in your project. You might as well just write

<style name="Widget.AppCompat.ActionBar.TabText" parent="@style/Base.Widget.AppCompat.ActionBar.TabText"> 

and it would work.

Thus, taking in account that Base.Widget.AppCompat.ActionBar.TabText is compiled in your project form the support library, you can add it with @style as prefix or without. However, @android:style/TextAppearance is from Android package, and that is why you have to specify @android: as a prefix.

I hope it is clear now

Upvotes: 1

davrog10
davrog10

Reputation: 91

Because developers in sources are inheriting style they defined themselves.

If you define

  <style name="CodeFont" parent="@android:style/TextAppearance">
        <item name="android:textColor">#00FF00</item>
    </style>

You don't need to write a parent from styles, but can just write

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

like so.

Explanation from here http://developer.android.com/guide/topics/ui/themes.html#Inheritance

Notice that there is no parent attribute in the <style> 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. This style then overrides the android:textColor property to make the text red. You can reference this new style as @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:

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

Upvotes: 0

Related Questions