Absolute layout: A layout that requires specifying the precise x, y coordinates of the child elements.
shortcoming:
Absolute layout lacks flexibility and is more difficult to maintain than other types of layout without absolute positioning, so it is not recommended.
Because of the absolute layout, the applications we develop need to be adapted to many models. If you use this absolute layout, the display may be normal on a 4-inch mobile phone, but if you switch to a 5-inch mobile phone, the display will be normal. Shifts and deformations may occur.
The current Android Studio will display a deprecated mark when used.
Common properties:
Attributes | effect |
---|---|
android:layout_width | Component width |
android:layout_height | Component height |
android:layout_x | Set the X coordinate of the component |
android:layout_y | Set the Y coordinate of the component |
These properties are relatively simple and easy to use.
Example:
<?xml version="1.0" encoding="utf-8"?>
<AbsoluteLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".AbsoluteLayoutActivity">
<TextView
android:layout_width="200dp"
android:layout_height="100dp"
android:layout_x="110dp"
android:text="绝对布局"
android:textSize="50dp"
android:gravity="center"/>
<ImageView
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_x="50dp"
android:layout_y="150dp"
android:src="@drawable/img_2">
</ImageView>
<ImageView
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_x="50dp"
android:layout_y="300dp"
android:src="@drawable/img">
</ImageView>
<ImageView
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_x="50dp"
android:layout_y="450dp"
android:src="@drawable/zy">
</ImageView>
<Button
android:layout_width="100dp"
android:layout_height="50dp"
android:layout_x="250dp"
android:layout_y="150dp"
android:background="@drawable/img_2"
android:text="按钮1">
</Button>
<Button
android:layout_width="100dp"
android:layout_height="50dp"
android:layout_x="250dp"
android:layout_y="300dp"
android:background="@drawable/img"
android:text="按钮2">
</Button>
<Button
android:layout_width="100dp"
android:layout_height="50dp"
android:layout_x="250dp"
android:layout_y="450dp"
android:background="@drawable/zy"
android:text="按钮3">
</Button>
<TextView
android:layout_width="200dp"
android:layout_height="100dp"
android:layout_x="110dp"
android:layout_y="550dp"
android:text="通过layout_x和layout_y来调节位置"
android:textSize="20dp"
android:textStyle="bold"
android:gravity="center"/>
</AbsoluteLayout>
Effect:
Absolute layout is the simplest layout among front-end layouts, but its flexibility is extremely poor and it does not have the ability to automatically adapt to the device resolution. Just like the layout set on a mobile phone, the layout on a tablet will be completely chaotic, so in daily development Absolute layout is rarely used.