Android self-study tour--layout resource file

Source: Internet
Author: User

This article is just looking at the translation and understanding of official help documents to make it easier for me to see them later.

What is the layout resource file? Layouts an XML file that defines the layout of an activity or component.

It generally looks like this:

<?XML version= "1.0" encoding= "Utf-8"?><ViewGroupxmlns:android= "Http://schemas.android.com/apk/res/android"Android:id= "@[+][package:]id/resource_name"Android:layout_height=["Dimension"| "Fill_parent" | "Wrap_content"] android:layout_width=["Dimension"| "Fill_parent" | "Wrap_content"] [viewgroup-specific attributes]>    <ViewAndroid:id= "@[+][package:]id/resource_name"Android:layout_height=["Dimension"| "Fill_parent" | "Wrap_content"] android:layout_width=["Dimension"| "Fill_parent" | "Wrap_content"] [view-specific attributes]>        <Requestfocus/>    </View>    <ViewGroup>        <View/>    </ViewGroup>    <includeLayout= "@layout/layout_resource"/></ViewGroup>

Note: The root node can be a viewgroup, view, or merge node, but only one has one root node, and it must have a Xml:android property to define the namespace.

Node Description:

<ViewGroup>

It is a container that can put a lot of view. There are many kinds of viewgroup, mainly including LinearLayout, Relativelayout and Framelayout.

Property:

Android:id

It is a unique identifying name for a node, and you can apply a viewgroup in the application with that identity name.

Android:layout_height

ViewGroup height, the value can be a dimension value, or it can be "fill_parent" or "wrap_content", it is necessary.

Android:layout_width

The width of the viewgroup, the value can be a dimension value, or it can be "fill_parent" or "wrap_content", it is necessary.

These are just a few of the basic properties of ViewGroup, and different viewgroup have many properties.

<view>

A UI component such as: TextView, Button, CheckBox.

Common properties are the same as viewgroup, and there are many properties that belong to different view.

<requestFocus>

Any view node can contain a modified property, which causes its parent element to get focus at initialization time, but only one node in each layout is allowed to have that property.

<include>

The label can be used to add other well-defined layout files to the current layout.

Property:

Layout

A reference to the layout resource is required.

Android:id

The resource ID, which overrides the ID of the current containing layout provided to the root view.

Android:layout_height

Overrides the height of the currently contained layout provided to the root view, only valid if the android:layout_width is defined at the same time.

Android:layout_width

Overrides the width of the currently contained layout provided to the root view, only valid if the android:layout_height is defined at the same time.

You can also override other properties in the include layout supported by the root view in include.

Another way to include a layout is to use Viewstub. It is a lightweight view that does not occupy any layout control until you explicitly inflate it and can use it through android:layout.

<merge>

Currently you want to use the ViewGroup already included in layout, and you want to add to the current ViewGroup through include, you can use the current element as the root node, the include layout in the

ViewGroup merges with the current viewgroup.

  Value for Android:id

The value of the definition change property is generally through "@+id/name" this form, "+" means this is a new ID, if this "name" does not exist, then the AAPT tool will create a new resource integer in the R.java class

In For example:

  

<android:id= "@+id/nametextbox"/>

This "nameTextBox" can now be used as the resource ID of the current textview. You can refer to TextView in Java code by using the resource ID in the following way:

Findviewbyid (R.id.nametextbox);

This code will return the TextView object.

  Value for Android: Layout_heigth and android:layout_width

The Heigth and width values can be values for the size units supported by Android (PX, SP, SP, PT, in, NM) or the following:

Value Descrption
Match_parent Sets the size of the control to be the same as the parent element, added in API8.
Fill_parent Sets the size of the current control to be the same as the parent element, which is obsolete above API8.
Wrap_content Set the control size to include the contents of the control (picture or text)

Instance:

  res/layout/main_activity.xml中的layout布局文件:

<?XML version= "1.0" encoding= "Utf-8"?><LinearLayoutxmlns:android= "Http://schemas.android.com/apk/res/android"Android:layout_width= "Fill_parent"Android:layout_height= "Fill_parent"android:orientation= "vertical" >    <TextViewAndroid:id= "@+id/text"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:text= "Hello, I am a TextView" />    <ButtonAndroid:id= "@+id/button"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:text= "Hello, I am a button" /></LinearLayout>

Load the layout in Acitivity's oncreate:

     Public void onCreate (Bundle savedinstancestate) {        super. OnCreate (savedinstancestate);        Setcontentview (r.layout.main_activity);    }

Android self-study tour--layout resource file

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.