Android control (button) Alignment Method implementation

Source: Internet
Author: User
Tags xml attribute

1. android: orientation
Layout direction. Horizontal is used to arrange all child elements horizontally from left to right. vertical is used to arrange all child elements vertically from top to bottom.

2. Differences between android: gravity and android: layout_gravity android: gravity specifies the alignment of the child element of this element.

Android: layout_gravity specifies the alignment of the element to its parent element.
For example:
In the following example, the android: gravity of linearlayout is set to right, with two child elements: Button01 and Button02.
Java code:Copy codeThe Code is as follows: <? Xml version = "1.0" encoding = "UTF-8"?>
<LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android"
Android: orientation = "vertical"
Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent"
Android: gravity = "right"
>
<Button android: text = "button01" android: id = "@ + id/Button01" android: layout_width = "wrap_content" android: layout_height = "wrap_content"> </Button>
<Button android: text = "button02" android: id = "@ + id/Button02" android: layout_width = "wrap_content" android: layout_height = "wrap_content"> </Button>
</LinearLayout>

LinearLayout in main. xml also has two child elements: Button01 and Button02. Android: layout_gravity of Button01 is set to left, and android: layout_gravity of Button02 is set to right"
Java code:Copy codeThe Code is as follows: <? Xml version = "1.0" encoding = "UTF-8"?>
<LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android"
Android: orientation = "vertical"
Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent"
>
<Button
Android: layout_gravity = "left"
Android: text = "button01 ″
Android: id = "@ + id/Button01 ″
Android: layout_width = "wrap_content"
Ndroid: layout_height = "wrap_content"> </Button>
<Button
Android: layout_gravity = "right"
Android: text = "button02 ″
Android: id = "@ + id/Button02 ″
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content">
</Button>
</LinearLayout>

FameLayout Layout
FrameLayout is the simplest layout object. It is customized as a blank standby area on your screen, and then you can fill in a single object-for example, an image you want to publish. All child elements are fixed in the upper left corner of the screen. You cannot specify a position for a child element in FrameLayout. The next child element directly overwrites the previous child element and blocks them in part or whole (unless the last child element is transparent ).

Xml attributes
1. Use an xml file to define the interface, and then the setContentView method of the Activity creates a real control object based on the definition in the xml file. For example, the xml file is a design drawing, and the setContentView is a production machine, which produces a variety of cups and washing utensils according to the drawings.
2. the xml attributes of FrameLayout come from three aspects: inherited, nested, and defined by the class itself.
3. For more information about the attributes, see the official documentation. The following are the attributes we encountered just now.
Java code:Copy codeThe Code is as follows: android: id
// This xml Attribute is inherited from the xml Attribute of the android. view class. It provides a unique identifier for framelayout. When you use it, you can use View. findViewById () or Activity. findViewById () to find it.
Android: layout_width: Layout width
Android: layout_height: Layout height
// Their values are fill_parent or wrap_content.
Fill_parent: fill the space of the parent container on the X axis or Y axis.
Wrap_content: the elements in framelayout can be as wide and high as possible,
// These two attributes are defined in the nested android. widget. FrameLayout class android. widget. FrameLayout. LayoutParams.
Android: background
Android: foreground

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.