一、線性布局
線性布局按照線性排列的方式進行布局,支持嵌套,具有靈活性和方便性。在實現線性布局時,需要設置其方向(水平或垂直),還可以設置gravity屬性來控制子視圖的位置和對齊方式。以下是一個簡單的線性布局代碼示例:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="center_vertical">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_launcher" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!" />
</LinearLayout>
在以上代碼中,LinearLayout的orientation屬性設置為horizontal,即水平方向排列子視圖。ImageView和TextView都放在LinearLayout中,ImageView和TextView均設置了layout_width和layout_height屬性,子視圖會根據這些屬性進行排列。
二、相對布局
相對布局按照相對的位置進行布局,它支持子視圖間相互定位,靈活性高。在實現相對布局時,需要設置子視圖之間的對齊方式、相對位置和間距等屬性。以下是一個簡單的相對布局代碼示例:
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_launcher"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
android:layout_toRightOf="@id/image"
android:layout_centerVertical="true" />
</RelativeLayout>
在以上代碼中,ImageView和TextView分別位於左側和右側,通過layout_toRightOf和android:layout_alignParentLeft屬性設置相對位置。
三、幀布局
幀布局從左上角開始,支持子視圖的堆疊,適合做小型的頁面。在實現幀布局時,需要注意子視圖的布局順序,後添加的子視圖會覆蓋前面的子視圖。以下是一個簡單的幀布局代碼示例:
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="centerCrop"
android:src="@drawable/background" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
android:layout_gravity="center" />
</FrameLayout>
在以上代碼中,ImageView和TextView都放在FrameLayout中,ImageView會覆蓋TextView。通過layout_gravity屬性可以控制TextView的位置和對齊方式。
四、表格布局
表格布局按照行和列的方式進行布局,支持跨行跨列,類似於HTML中的table布局。在實現表格布局時,需要設置行列數、子視圖的位置和對齊方式等屬性。以下是一個簡單的表格布局代碼示例:
<TableLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TableRow>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Name:"
android:padding="5dp" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter your name" />
</TableRow>
<TableRow>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Age:"
android:padding="5dp" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter your age" />
</TableRow>
</TableLayout>
在以上代碼中,通過TableRow設置每行的子視圖,TextView和EditText分別位於左側和右側。通過padding屬性設置內邊距。
五、約束布局
約束布局是最為強大的布局方式,支持子視圖之間的依賴關係、循環依賴以及動態的約束條件。在實現約束布局時,需要設置每個子視圖之間的約束關係,如相對位置、邊距和對齊方式等屬性。以下是一個簡單的約束布局代碼示例:
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent" />
<EditText
android:id="@+id/input"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter your name"
app:layout_constraintTop_toBottomOf="@id/label" />
</androidx.constraintlayout.widget.ConstraintLayout>
在以上代碼中,TextView和EditText都在ConstraintLayout中,TextView相對於parent居中顯示,EditText相對於TextView的下方顯示。
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-hant/n/197365.html
微信掃一掃
支付寶掃一掃