LinearLayout
LinearLayout是线性布局控件,它包含的子控件将以横向或竖向的方式排列。
LinearLayout常用属性
android:orientation= “vertical”
——该属性决定它子类控件的排布方式(vertical:垂直 horizontal:水平)
android:gravity =”center”
——该属性决定它子类的XY的位置
常用到的几个属性值:
1>center_vertical:垂直居中
2>center_horizental:水平居中
3>center:水平垂直都居中
4>right:子类控件位于当前布局的右边
5>left:子类控件位于当前布局的左边
6:>bottom:子类控件位于当前布局的下面
子类控件在LinearLayout中常用到的属性
android:layout_gravity = "bottom"
<!--指本身在父容器中XY的一个位置-->
android:layout_weight = "1"
<!--指本身控件占当前父容器的一个比例,可以是小数-->
注意:当两个个容器的高度和宽度都是match_parent的时候layout_weight值与真实比例成反比。
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center_horizontal|center_vertical">
<!--android:gravity="center"效果与上一语句相同-->
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
</LinearLayout>
效果图
子类控件常用属性代码
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<Button
android:layout_gravity="right"
android:layout_weight="2"
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
<Button
android:layout_weight="1"
android:layout_gravity="center_horizontal"
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
</LinearLayout>
效果图
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<Button
android:layout_weight="2"
android:id="@+id/button"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Button" />
<Button
android:layout_weight="1"
android:id="@+id/button2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Button" />
</LinearLayout>
效果图
布局嵌套布局
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="button1"/>
<Button
android:id="@+id/button5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:layout_marginLeft="20dp"
android:text="button2" />
</LinearLayout>
</LinearLayout>
效果图