Listing 37 XML layout resource file for reviewdetailxml

<?xml version= "1.0" encoding= "utf-8"?> O Define root View element

<RelativeLayout <1-'

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

android: layout_width= "fill_parent" I C Define android: layout_height= "fill_parent " r"^ LayoutParams android:gravity="center_horizontal"

android: padding= "10px" <-1 Define View android. setVerticalScrollBarEnabled= "true" Q parameters in XML

<ImageView android : id= "@+id/review_image" <-1 Include child

E element with ID

F Reference another -,_I resource

Reference a android:layout_width="100px" android:layout_height="100px" android:layout_marginLeft="10px" android:layout_marginBottom="5px" />

<TextView android :id="@+id/name_detail" android:layout_width="fill_parent" android:layout_height="wrap_content" android :layout_below="@id/review_image" android:layout_marginLeft="10px" android:layout_marginBottom="5px" style="@style/intro_blurb" />

<TextView android : id= "@+id/rating_label_detail" G style for a View android :layout_width="wrap_content" android:layout_height="wrap_content" android :layout_below="@id/name_detail" android:layout_marginLeft="10px"

android:layout_marginBottom="5px" style="@style/label"

android :text="@string/rating_label" />

. . . remainder of file omitted for brevity </RelativeLayout>

In this file we are using a RelativeLayout O- This is the ViewGroup at the root of the View tree. LayoutParams are then also defined in XML using the android: layout_[attribute] convention (where [attribute] refers to a layout attribute) ©. Along with layout, other View-related attributes can also be defined in XML with counterpart XML attributes to the methods available in code, such as android: padding, which is analogous to setPadding() ©.

After the RelativeLayout parent itself is defined, the child View elements are added. Here we are using an ImageView and multiple TextView components. Each of the components is given an ID using the form android:id="@+id/[name]" Q. When an ID is established in this manner, an int reference is defined in the resource table and named with the specified name. This allows other components to reference the ID by the friendly textual name.

Once views are defined as resources, the Activity method findViewById() can be used to obtain a reference to a particular View using the name. That View can then be manipulated in code. For example, in listing 3.6 we grabbed the rating TextView as follows:

rating = (TextView) findViewById(R.id.rating_detail).

This inflates and hands off the rating_detail element we saw in listing 3.7. Note that child views of layout files end up as id type in R.java (they are not R.layout.name; rather they are R.id.name, even though they are required to be placed in the res/layout directory).

The properties for the View object are all defined in XML, and this includes the layout. Because we are using a RelativeLayout we use attributes that place one View relative to another, such as below or toRightOf. This is done with the android: layout_below="@id/[name] syntax ©. The @id syntax is a way to reference other resource items from within a current resource file. Using this approach you can reference other elements defined in the file you are currently working on or other elements defined in other resource files.

Some of our views represent labels, which are shown on the screen as is and are not manipulated in code, such as rating_label_detail. Others we will populate at runtime; these don't have a text value set, such as name_detail. The elements that we do know the values of, the labels, are defined with references to externalized strings.

The same approach is applied with regard to styles, using the syntax style="@style/ [stylename]" ©. Strings, styles, and colors are themselves defined as resources in another type of resource file.

0 0

Post a comment