-
Notifications
You must be signed in to change notification settings - Fork 54
Using attributes
Each style uses attributes to provide values to views. Here's an example XML with a view declaration:
<LinearLayout>
<ImageView
style="@style/ImageView.Icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:srcCompat="@drawable/ic_airplanemode_on_24px" />
</LinearLayout>The enclosing layout is invalid because it needs at least layout_width and layout_height attributes to be parsed correctly. Let's forget about for the sake of simplicity. This view has an inline style with four attributes:
-
style="@style/ImageView.Icon"-styleattribute is a special attribute parsed by the framework. It's used to change apply a style defined in an external XML file. There's a couple of special tags and attributes without any prefixes:layout,fragment,include,dataand probably a couple more. -
android:layout_width/_height="wrap_content"- attribute usages prefixed with android are defined by the Android framework. You can reuse them in your custom views if you wish. Attribute names prefixed with layout_ by convention are used by enclosing layouts. You can learn how to add your own layout attributes here.wrap_contentis an enum - one of many types of attributes. Attributes can reference values, resources, styles and many more. -
app:srcCompat="@drawable/ic_airplanemode_on_24px"- this is an attribute added by a library. Theappprefix is declared somewhere in the layout and can have any name.srcCompatis an attribute of typedrawableand is used here to add support for vector drawables. It references a file from res/drawable folder.
Another example:
<layout>
<TextView
android:layout_width="120dp"
android:layout_height="32dp"
guide_htmlText="Select Dates"
android:textColor="?android:attr/textColorPrimary"
android:textAppearance="@style/TextAppearance.Body1" />
</layout>This view is enclosed in layout tag - it's a data binding declaration. This tag has some special meaning and features.
- This view also uses
layout_widthandlayout_height, but this time with values of typedimension. Thedpmeans that the value is expressed in device independent pixels. Some of the attributes have more than one type. -
guide_htmlText="Select Dates"- this is a library attribute and is used without any namespace prefix. It's a data binding attribute handled by a binding adapter. -
android:textColor="?android:attr/textColorPrimary"- this attribute has a theme reference value. The question mark starts a theme reference. Theandroid:part is a namespace and is empty for custom attributes. Theattr/textColorPrimarypart declares an attribute present in the current theme to take a value from. -
android:textAppearance="@style/TextAppearance.Body1"- a framework's attribute ofreferencetype. Is used to change text style of text views. Here it references astyleresource of nameTextAppearance.Body1.
Resources are a very broad topic. Even a very simple application usually needs at least a theme, a couple of drawables (icons) and uses styles and colors declared somewhere. Read more about resources on developer.android.com.