簡單線性佈局
線性佈局最簡單,就是從左到右或者從上到下依次排列的佈局,常見如登錄頁面
<?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"
android:orientation="vertical"
android:padding="4dp">
<EditText
android:id="@+id/editTextName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minLines="3"
android:text="姓名" />
<EditText
android:id="@+id/editTextPasword"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minLines="3"
android:text="密碼" />
<Button
android:id="@+id/button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="登錄" />
</LinearLayout>
效果如下
嵌套線性佈局
如果想實現姓名、密碼在一行,可以嵌套兩個線性佈局,外層是垂直線性,內層的姓名、密碼是水平的線性佈局。如下:
<?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"
android:orientation="vertical"
android:padding="4dp">
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:padding="4dp">
<EditText
android:id="@+id/editTextName"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:minLines="3"
android:layout_weight="1"
android:text="姓名" />
<EditText
android:id="@+id/editTextPasword"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:minLines="3"
android:layout_weight="1"
android:text="密碼" />
</LinearLayout>
<Button
android:id="@+id/button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="登錄" />
</LinearLayout>
效果如下: