(original) "Android Programming Authority Guide" learning note 01--Android app first experience--004

Source: Internet
Author: User

Let's start by learning how to define layouts using XML code.

In the current Activity_quic.xml file, the default activity layout is defined. The default layout of the app often changes, but the XML layout file is always similar to the code listing 1-1 file.

<RelativelayoutXmlns:android= "Http://schemas.android.com/apk/res/android"Xmlns:tools= "Http://schemas.android.com/tools"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent"Android:paddingbottom= "@dimen/activity_vertical_margin"Android:paddingleft= "@dimen/activity_horizontal_margin" Android:paddingright=" @dimen/activity_horizontal_ Margin " Android:paddingtop Tools:context=". Quizactivity "> < textview android:layout_width = "wrap_content"  Android:layout_height= "Wrap_content"  Android:text= "@string/hello_world" /></ Relativelayout>         

Code Listing 1-1 Code listing 1-1: Default activity Layout (Activity_quiz.xml)

First, we notice that the Activity_quiz.xml file no longer contains the following code for the version declaration and file encoding:

<?  ?>

After the ADT21 development version, the line code is no longer needed for Android layout files. However, in many cases, you may also see it.

The layout of the app activity defines two components (widgets) By default: Relativelayout and Textview.

  A component is a building block that makes up the user interface. buttons, text input controls, and selection boxes are all components.

The Android SDK has a number of components built in to get the user interface and behavior you need by configuring various components. Each component is a specific instance of the view class or its subclasses, such as TextView or button.

The Quizactivity user interface requires the following five components:

1. A vertical linerlayout component;

2. One TextView component

3. A horizontal Linerlayout component;

4. Two button components.

  

A component that is laid out and displayed on the screen.

Below we define these components in the Activity_quiz.xml file.

<LinearLayoutxmlns:android= "Http://schemas.android.com/apk/res/android"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent"android:gravity= "Center"android:orientation= "vertical" >            <TextViewAndroid:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"android:padding= "24DP"Android:text= "@string/question_text" />        <LinearLayoutAndroid:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"android:orientation= "Horizontal" >            <ButtonAndroid:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:text= "@string/true_button" />                <ButtonAndroid:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:text= "@string/false_button" />            </LinearLayout>    </LinearLayout>

Code Listing 1-2: Defining components in an XML file (activity_quiz.xml)

It is important to note that the development tool cannot validate the layout XML content, avoid typing or spelling errors.

Depending on the version of the tool you are using, code that starts with Android:text three lines may produce an error message. Ignore them temporarily and process them later.

As you can see, the element name is the type of the component.

Each element has a set of XML attributes. Properties can be thought of as instructions on how to configure a component.

  

View Hierarchy

  The component is contained in the hierarchy of the View object, which is the view hieratchy. Figure 1-10 shows the view hierarchy for the XML layout shown in Listing 1-2.

  

As can be seen from the above, the root element is a linearlayout component. As the root element, the LinearLayout component must specify the namespace property of the Android XML resource file as: Http://schemas.android.com/apk/res/android.

The LinearLayout component inherits from the ViewGroup component of the view subclass. The ViewGroup component is a special component that contains and configures other components. To lay out the components in a row or row of styles, use the LinearLayout component. Other ViewGroup subclasses also include Framelayout, Tablelayout, and Relativelayout.

If a component is contained in a viewgroup, the component and ViewGroup form a parent-child relationship. The root LinearLayout has two sub-components: TextView and LinearLayout. The linearlayout itself as a subcomponent also has two button subcomponents.

Component Properties
Let's take a look at some of the common properties of configuration components.
1. Android:layout_width and Android:layout_height Properties
Android:layout_width and Android:layout_height properties are required for almost every type of component. They are usually set to one of the following two property values.
? Match_parent: The view is the same size as its parent view.
? Wrap_content: The view automatically resizes according to its content.
(There was also a Fill_parent property value, equivalent to Match_parent, which is deprecated.)

The TextView component is slightly larger than the text content area it contains, which is primarily the function of the android:padding= "24DP" property. This property tells the component to increase the amount of space that is specified in addition to the content itself when determining the size . This will leave a certain amount of space between the problems displayed on the screen and the buttons. (DP is density-independent pixel, which refers to density-independent pixels, and the eighth chapter introduces the concept of it.) )

2. Android:orientation Properties

The Android:orientation property is a property of two linearlayout components that determines whether the component is placed horizontally or vertically . In the XML above, the root linearlayout is vertical and the sub-linearlayout is horizontal.

The order in which the LinearLayout subcomponents are defined determines the order in which they appear on the screen. In a vertical linearlayout, the first defined subassembly appears at the top of the screen. In the horizontal linearlayout, the first defined subassembly appears on the leftmost side of the screen. (If the device language is displayed right-to-left, the first defined subassembly appears at the far right of the screen.) )

3. Android:text Properties

The TextView and button components have the Android:text property. This property specifies the text content displayed by the component.

Note that the Android:text property value is not a string literal, but rather a reference to a string resource (string resources).

  String resources are contained in a separate XML file named strings, although it is often not a good method to hardcode the text properties of a component, such as android:text= "True". It is a good practice to place text content in a separate string resource XML file and then reference them.

The string resource that needs to be referenced in the Activity_quiz.xml file does not currently exist. Now let's add these resources.

(original) "Android Programming Authority Guide" learning note 01--Android app first experience--004

Related Article

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.