What Attributes Are Supported for What Elements

Android uses the Layoutlnflater class at run time to load an XML layout resource and translate it into visual elements. By default, all widget class names are supported directly as tags, but a full list of supported tags and attributes is listed in the R.styleable reference page. However, the attribute names are somewhat obscure. If an underscore appears in the name, this indicates that it is an attribute — typically of the element before the underscore. So, for example, EditText_autoText means that the <EditText> tag supports an attribute autoText. When you actually use the attribute in that element, use only the portion after the last underscore, and prefix the attribute with the prefix "android:". So, for example, if R.styleable lists the following values:

• TextView_lines

• TextView_maxlines

You could create an element like this:

<TextView android:lines="10" android:maxlines="2 0"/>

This would create a TextView object and set its lines and maxlines properties. Attributes come from three sources:

• Attributes exposed directly by the element. For example, TextView supports TextView_text, as discussed above.

• Attributes exposed by all the superclasses of that element. For example, the TextView class extends the View class, so the <TextView> element supports all the attributes that the <View> element exposes — a long list, including View_paddingBottom and View_scrollbars. These too are used without the class name: <TextView android:paddingBottom="20" android:scrollbars="horizontal" />.

• Attributes of the object's ViewGroup.LayoutParams subclass. All View objects support a LayoutParams member (see Declaring Layout). To set properties on an element's LayoutParams member, the attribute to use is "android:layout_/ayoutParamsProperty". For example: android:layout_gravity for an object wrapped by a <LinearLayout> element. Remember that each LayoutParams subclass also supports inherited attributes. Attributes exposed by each subclass are given in the format someLayoutParamsSubc/ass_Layout_layout_someproperty. This defines an attribute "android:layout_someproperfy". Here is an example of how Android documentation lists the properties of the LinearLayout.LayoutParams class:

• LinearLayout_Layout // The actual object — not used.

• LinearLayout_Layout_layout_gravity // Exposes a gravity attribute

• LinearLayout_Layout_layout_height // Exposes a height attribute

• LinearLayout_Layout_layout_weight // Exposes a weight attribute

• LinearLayout_Layout_layout_width // Exposes a width attribute

Here is an example that sets some of these values on a few objects, including direct attributes, inherited attributes, and LayoutParams attributes:

<?xml version="1.0" encoding="utf-8"?> <!-- res/main_screen.xml -->

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

android:orientation="vertical" // The object's own orientation property android:padding="4" // Inherited View property android:gravity="center" // The object's own property android:layout_width="fill_parent" // Parent object's LinearLayout.LayoutParams.width android:layout_height="fill_parent"> // Parent object's LinearLayout.LayoutParams.height

<TextView android:layout_width="fill_parent" // TextView.LayoutParams.width android:layout_height="wrap_content" // TextView.LayoutParams.height android:layout_weight="0" // TextView.LayoutParams.weight android:paddingBottom="4" // TextView.paddingBottom android:text="@string/redirect_getter"/> // TextView.text

<EditText id="@+id/text"

android:layout_width="fill_parent" // EditText.LayoutParams.width android:layout_height="wrap_content" // EditText.LayoutParams.height android:layout_weight="0" //

EditText.LinearLayoutParams.weight android:paddingBottom="4"> // EditText.paddingBottom

<requestFocus /> </EditText>

android:layout_width="wrap_content" // Button.LayoutParams.width android:layout_height="wrap_content" // Button.LayoutParams.height android:text="@string/apply" /> // TextView.text


0 0

Post a comment