XML @style in parent

In examples of android templates, style-style is defined as follows

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

but in android sources i find

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

What is the difference when I prefix with @style and @android: style or not?

+5
source share
2 answers

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

For example, if you are trying to get color:

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

The result is the color Android holo_red_dark. You do not have this color defined in your project.

 android:background="@color/my_red_color" 

This will give you your "my_red_color" defined in your project.

The same goes for styles.

EDIT: The thing is, between

 parent="@style/MyStyle" 

and

 parent="MyStyle" 

for the style compiled in your project. You could just write

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

and it will work.

Thus, considering that Base.Widget.AppCompat.ActionBar.TabText compiled in your design form in the support library, you can add it with @style as a prefix or without it. However, @android:style/TextAppearance is in the Android package, so you need to specify @android: as the prefix.

Hopefully now clear.

0
source

Since the developers in the sources inherit the style, they themselves determined.

If you define

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

You do not need to write a parent style from styles, but you can just write

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

.

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

Note that the <style> does not have a parent attribute, but since the name attribute starts with the name of the CodeFont style (which is the style you created), this style inherits all the properties of the style from this style. This style then overrides the android: textColor property so that the text is red. You can reference this new style as @ style / CodeFont.Red.

You can continue to inherit as many times as you want name chains with periods. For example, you can extend CodeFont.Red to be larger with:

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

Source: https://habr.com/ru/post/1243207/


All Articles