LinearLayout的特點:組件一個挨着一個,可以縱向也可以橫向;但是不會換行;當空間排列到屏幕的盡頭時,多與組件將不會被顯示.
必須屬性:android:orientation:vertical (垂直方向) 、horizontal(水平方向)
一些常用屬性:
android:gravity=”center”————設置的是控件自身的子類控件的對齊方式;
android:layout_gravity=”center”—–設置控件本身相對於父控件的對齊方式;
注意:
- *如果線性佈局的方向是垂直的,只有水平方向的設置起作用,垂直方向的設置不起作用
- *如果線性佈局的方向是水平的,只有垂直方向的設置起作用,水平方向的設置不起作用
android:layout_weight=”權數/權屬和”—– 權重;給控件分配剩餘空間
示例:
1.水平排列按鈕
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button 1" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button 2" />
</LinearLayout>
2.綜合應用
線性佈局可以進行嵌套,則可以製造出更加豐富的效果
嵌套方式:
<?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="horizontal" >
<LinearLayout >
..
</LinearLayout>
<LinearLayout>
..
</LinearLayout>
</LinearLayout>
先貼圖:
實現此效果的代碼:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.example.demo2.MainActivity" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="25dp"
android:gravity="center"
android:orientation="horizontal">
<TextView
android:id="@+id/t1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="10dp"
android:layout_weight="1"
android:singleLine="true"
android:text="打開。。。" />
<TextView
android:id="@+id/t2"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_weight="1"
android:gravity="right"
android:text="Ctrl+O" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="25dp"
android:gravity="center"
android:orientation="horizontal" >
<TextView
android:id="@+id/t3"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_marginLeft="10dp"
android:layout_weight="1"
android:text="保存。。。" />
<TextView
android:id="@+id/t4"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_weight="1"
android:gravity="right"
android:text="Ctrl+S" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="25dp"
android:gravity="center"
android:orientation="horizontal" >
<TextView
android:id="@+id/t5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_weight="1"
android:text="另存爲。。。" />
<TextView
android:id="@+id/t6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="right"
android:text="Shift+Ctrl+S" />
</LinearLayout>
<View
android:layout_height="1dp"
android:layout_width="match_parent"
android:background="#686868"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="25dp"
android:gravity="center"
android:orientation="horizontal" >
<TextView
android:id="@+id/t7"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_marginLeft="10dp"
android:textIsSelectable="true"
android:textColorHighlight="#B6292B"
android:layout_weight="1"
android:text="導入。。。" />
<TextView
android:id="@+id/t8"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_weight="1"
android:gravity="right"
android:text=""
/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="25dp"
android:gravity="center"
android:orientation="horizontal" >
<TextView
android:id="@+id/t9"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_marginLeft="10dp"
android:layout_weight="1"
android:text="導出。。。" />
<TextView
android:id="@+id/t10"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_weight="1"
android:gravity="right"
android:text="Ctrl+R" />
</LinearLayout>
<View
android:layout_height="1dp"
android:layout_width="match_parent"
android:background="#686868"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="30dp"
android:gravity="center"
android:orientation="horizontal" >
<TextView
android:id="@+id/t11"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="10dp"
android:layout_weight="1"
android:text="退出" />
<TextView
android:id="@+id/t12"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_weight="1"
android:gravity="right"
android:text=""
/>
</LinearLayout>
<View
android:layout_height="1dp"
android:layout_width="match_parent"
android:background="#686868"/>
<EditText
android:id="@+id/et1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:maxLength="15"
android:digits="123456789"
/>
<Button
android:id="@+id/button1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="按鈕" />
</LinearLayout>
初學者,希望對大家有用!!!重點內容