AbsoluteLayout通过指定组件的确切X、Y坐标来确定组件的位置。
下面代码用于演示AbsoluteLayout的使用
代码
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<AbsoluteLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<Button
android:text="A"
android:id="@+id/Button01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="10dp"
android:layout_y="20dp"></Button>
<Button
android:text="B"
android:id="@+id/Button02"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="100dp"
android:layout_y="20dp"></Button>
<Button
android:text="C"
android:id="@+id/Button03"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="10dp"
android:layout_y="80dp"></Button>
<Button
android:text="D"
android:id="@+id/Button04"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="100dp"
android:layout_y="80dp"></Button>
</AbsoluteLayout>
</LinearLayout>
上述代码使用< AbsoluteLayout >元素来定义绝对布局,该布局中有4个按钮,每个按钮的位置都是通过X、Y轴坐标进行指定的,其中 layout_x属性用于指定元素的X轴坐标,ayout_y属性用于指定元素的Y轴的坐标。
在LayoutActivity中使用absolutelayout.xml进行布局,相关代码如下所示。
package com.qst.demo2;
import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
public class LayoutActivity extends AppCompatActivity {
public void onCreate(Bundle savedIntanceState) {
super.onCreate(savedIntanceState);
setContentView(R.layout.absolutelayout);
}
}
运行结果