线性布局:
线性布局特点,同一级组件之间没有互相依赖
按照添加顺序依次排列,其中线性布局最重要的属性是android:orientation
通过该属性可以指定水平方向排列还是纵向排列
orientation="vertical"水平方向
orientation="horizontal" 垂直方向
注意: Android 的线性布局不会换行,当组件一个挨着一个的排列到头之后,
剩下的组件将不会被显示出来
<?xml version="1.0" encoding="utf-8"?>
<!--线性布局特点,同一级组件之间没有互相依赖,-->
<!--按照添加顺序依次排列,其中线性布局最重要的属性是android:orientation-->
<!--通过该属性可以指定水平方向排列还是纵向排列
orientation="vertical"水平方向
orientation="horizontal" 垂直方向
-->
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="250dp"
android:orientation="horizontal">
<TextView
android:layout_width="96dp"
android:layout_height="match_parent"
android:background="#b2dfdb"
android:text="第一块"/>
<TextView
android:layout_width="96dp"
android:layout_height="match_parent"
android:background="#80cbc4"
android:text="第二块"/>
<TextView
android:layout_width="96dp"
android:layout_height="match_parent"
android:background="#4db6ac"
android:text="第三块"/>
<TextView
android:layout_width="96dp"
android:layout_height="match_parent"
android:background="#26a69a"
android:text="第四块"/>
<TextView
android:layout_width="96dp"
android:layout_height="match_parent"
android:background="#26a69a"
android:text="第五块"/>
<TextView
android:layout_width="96dp"
android:layout_height="match_parent"
android:background="#CDDC39"
android:text="第六块"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="68dp"
android:background="#b2dfdb"
android:text="第一块"/>
<TextView
android:layout_width="match_parent"
android:layout_height="68dp"
android:background="#80cbc4"
android:text="第二块"/>
<TextView
android:layout_width="match_parent"
android:layout_height="68dp"
android:background="#4db6ac"
android:text="第三块"/>
<TextView
android:layout_width="match_parent"
android:layout_height="68dp"
android:background="#26a69a"
android:text="第四块"/>
</LinearLayout>
</LinearLayout>


线性布局 gravity(重力)属性:
设置布局管理器内组件的对齐方式,该属性值:
top(顶部对齐) 、bottom 、left 、right 、center_vertical(垂直方向居中) 、 fill_vertical(垂直方向填充) 、 center_horizontal(水平方向居中) 、 fill_horizontal(水平方向填充) 、center(垂直与水平方向都居中) 、 fill (填充)、 clip_vertical(垂直方向裁剪) 、 clip_horizontal(水平方向裁剪) 。可同时指定多种对其方式的组合,中间用“|”连接,
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="bottom">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="250dp"
android:orientation="horizontal">
<TextView
android:layout_width="96dp"
android:layout_height="match_parent"
android:background="#b2dfdb"
android:text="第一块"/>
<TextView
android:layout_width="96dp"
android:layout_height="match_parent"
android:background="#80cbc4"
android:text="第二块"/>
<TextView
android:layout_width="96dp"
android:layout_height="match_parent"
android:background="#4db6ac"
android:text="第三块"/>
</LinearLayout>

布局中layout-gravity与gravity的区别
1:两者可设置的值是相同的
2: gravity是设置自身内部元素的对齐方式。
layout_gravity是设置自身相当于父容器的对齐方式。
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/父组件"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#F44336"
android:gravity="bottom"
>
<LinearLayout
android:id="@+id/子组件"
android:layout_width="match_parent"
android:layout_height="250dp"
android:orientation="horizontal"
>
<TextView
android:id="@+id/ws1"
android:layout_width="96dp"
android:layout_height="150dp"
android:background="#b2dfdb"
android:gravity="bottom"
android:layout_gravity="top"
android:text="第一块"/>
<TextView
android:id="@+id/ws2"
android:layout_width="96dp"
android:layout_height="150dp"
android:background="#80cbc4"
android:text="第二块"/>
<TextView
android:id="@+id/ws3"
android:layout_width="96dp"
android:layout_height="150dp"
android:background="#4db6ac"
android:text="第三块"/>
</LinearLayout>

注解:
顶级父组件设置了android:gravity="bottom"规定了其内部组件(元素)的对齐方式 在子组件中有设有三个TextView组件android:gravity="bottom"设置了其内部内容的对齐方式android:layout_gravity="top"设置了组件id为ws1的TextView组件相对于其直接父组件的排列方式。
本文详细解析了Android线性布局的特点及使用方法,包括水平和垂直排列方式、重力属性的作用,以及layout-gravity与gravity的区别,帮助开发者掌握布局技巧。

3982

被折叠的 条评论
为什么被折叠?



