SoFunction
Updated on 2025-04-09

Detailed explanation of the Android control (button) alignment method implementation

1,android:orientation
Layout direction. horizontal is to arrange all child elements from left to right in horizontal direction, while vertical is to arrange all child elements from top to bottom in vertical direction.

2. The difference between android:gravity and android:layout_gravity android:gravity specifies the alignment of the child elements of this element relative to it.

android:layout_gravity specifies the alignment of this element with respect to its parent element.
For example:
The android:gravity of linearlayout here is set to right, and there are two child elements Button01 and Button02.
Java code:
Copy the codeThe code is as follows:

<?xml version=”1.0″ encoding=”utf-8″?>
<LinearLayout xmlns:android=”/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>

The LinearLayout in this also has two child elements Button01 and Button02. Button01's android:layout_gravity is set to "left", Button02's android:layout_gravity is set to "right"
Java code:
Copy the codeThe code is as follows:

<?xml version=”1.0″ encoding=”utf-8″?>
<LinearLayout xmlns:android=”/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” a
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 easiest layout object. It is customized as a blank spare area on your screen, where you can then fill it with a single object—for example, a picture you want to post. All child elements will be fixed in the upper left corner of the screen; you cannot specify a position for a child element in FrameLayout. The latter child element will be covered and filled directly on the previous child element, partially or completely blocking them (unless the latter child element is transparent).

xml attribute
1. Use the 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, xml files are design drawings, and setContentView is a production machine, which produces various cups and washing tools according to the drawings.
2. The xml attribute of FrameLayout comes from three places: inherited, defined by nested class, and defined by its own class.
3. The properties can be found in the official documentation. Below are a few attributes I just encountered.
Java code:
Copy the codeThe code is as follows:

android:id
//This xml attribute is an xml attribute inherited from the class. It provides a unique identifier for framelayout, and after that, when the program wants to use it, you can use() or() to find it.
android:layout_width: width of layout
android:layout_height: high layout
//Their values ​​are fill_parent or wrap_content.
fill_parent: The space that fills the parent container on the x-axis or y-axis.
wrap_content: The elements in framelayout have as much width and height as they are.
//These two properties are defined in nested classes.
android:background:background
android:foreground: foreground