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>
初学者,希望对大家有用!!!重点内容