android—-线性布局

时间:2021-01-19 15:16:13

android五大布局之线性布局。

  1.线性布局的特点:各个子元素彼此连接,中间不留空白

  

  而今天我们要讲解的就是第一个布局,LinearLayout(线性布局),我们屏幕适配的使用

  用的比较多的就是LinearLayout的weight(权重属性),下面写个案例来表明

  

<?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" > <LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight=""
android:background="#FFC90E"
> </LinearLayout> <LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight=""
android:orientation="vertical"
android:background="#DA70D6"
> <LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="" >
</LinearLayout> <LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight=""
android:background="#66CDAA"
android:orientation="horizontal"
> <TextView
android:id="@+id/txt1"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:background="#FFF663"
android:layout_weight="" />
<TextView
android:id="@+id/txt2"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:background="#E82917"
android:layout_weight="" />
<TextView
android:id="@+id/txt3"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:background="#FFF663"
android:layout_weight="" /> </LinearLayout> </LinearLayout>
</LinearLayout>

效果图为:

android—-线性布局

android:layout_gravity 本元素相对于父元素的重力方向

android:gravity 本元素所有子元素的重力方向

android:orientation 线性布局以列或行来显示内部子元素

android:layout_weight 子元素对未占用空间水平或垂直分配权重值

当 android:orientation="vertical" 时, 只有水平方向的设置才起作用,垂直方向的设置不起作用。即:left,right,center_horizontal 是生效的。!!!!

当 android:orientation="horizontal" 时, 只有垂直方向的设置才起作用,水平方向的设置不起作用。即:top,bottom,center_vertical 是生效的。!!!!

android:layout_gravity 和 android:gravity 的区别

android:gravity对元素本身起作用-本身元素显示在什么位置

android:layout_gravity相对与它的父元素-元素显示在父元素的什么位置。

如:Button控件

android:layout_gravity 表示button在界面上的位置

android:gravity表示button上的字在button上的位置。

可选值[多选时用“|”分开]

top、bottom、left、right、center_vertical、fill_vertical、center_horizontal、fill_horizontal、center、fill、clip_vertical。

top 将对象放在其容器的顶部,不改变其大小.

bottom 将对象放在其容器的底部,不改变其大小.

left将对象放在其容器的左侧,不改变其大小.

right将对象放在其容器的右侧,不改变其大小.

center_vertical 将对象纵向居中,不改变其大小.

垂直对齐方式:垂直方向上居中对齐。

fill_vertical 必要的时候增加对象的纵向大小,以完全充满其容器. 垂直方向填充

center_horizontal 将对象横向居中,不改变其大小水平对齐方式:水平方向上居中对齐

fill_horizontal 必要的时候增加对象的横向大小,以完全充满其容器. 水平方向填充

center 将对象横纵居中,不改变其大小.

fill 必要的时候增加对象的横纵向大小,以完全充满其容器.

clip_vertical 附加选项,用于按照容器的边来剪切对象的顶部和/或底部的内容. 剪切基于其纵向对齐设置:顶部对齐时,剪切底部;底部对齐时剪切顶部;除此之外剪切顶部和底部.垂直方向裁剪

clip_horizontal 附加选项,用于按照容器的边来剪切对象的左侧和/或右侧的内容. 剪切基于其横向对齐设置:左侧对齐时,剪切右侧;右侧对齐时剪切左侧;除此之外剪切左侧和右侧.水平方向裁剪

例子

TextView要让文本垂直/水平居中显示,有两种情况需要考虑:

1、layout_width/layout_height为wrap_content,此时要让TextView在父控件上居中显示,必须设置layout_gravity=”center”。

2、layout_width/layout_height为fill_parent,此时由于TextView已占据父窗体所有空间,必须设置gravity=”center”。