您的位置:首页 > 移动开发 > Android开发

android线性布局__LinearLayout的简单学习

2016-06-19 23:20 696 查看
LinearLayout是一个view组(view group),其包含的所有子view都以一个方向排列,水平方向(horizontal)或者是垂直方向(vertical),其属性是orientation。

LinearLayout的常用属性

layout_width//布局的宽度,
有三个属性match_parent、fill_parent、wrap_content.其中match_parent、fill_parent填充整个父布局,wrap_content是组件本身的大小,当然也可以自定义 “20dp”的形式
layout_height//布局的高度,
属性和上面layout_width的相同
orientation//设置布局方向
horizontal表示水平方向,vertical表示垂直方向
background//设置背景
可以使用图片或者直接使用颜色(#ffff0000)
layout_gravity//是本元素相对于父元素的对齐方式
gravity//是本元素所有子元素的对齐方式,设置在父元素上,
多个值用|隔开(right|top)右上


实例1:

orientation垂直方向(vertical)

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#00abac"
android:text="上面"
android:textSize="52dp" />

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#abac00"
android:text="下面"
android:textSize="52dp" />
</LinearLayout>


效果:



实例1:

orientation水平方向(horizontal)

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">

<TextView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:background="#00abac"
android:text="左面"
android:textSize="52dp" />

<TextView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:background="#abac00"
android:text="右面"
android:textSize="52dp" />
</LinearLayout>


效果:



LinearLayout的layout_weight(权重)属性

layout_weight是用来等比例化分区域大小的,

layout_weight简单的使用:



当为左面设置layout_weight=”2”时,有下图效果



这时候左面区域栈的比例为2/3,右面占的就是1/3

上面是宽度是wrap_content的情况下

下面讨论宽度在match_parent的情况下的场景

如下面的代码:

<?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">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#00abac"
android:text="左面"
android:textSize="52dp" />
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="2"
android:background="#ab00ac"
android:text="中间"
android:textSize="52dp" />
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="3"
android:background="#abac00"
android:text="右面"
android:textSize="52dp" />
</LinearLayout>


得到下面的效果:



我们发现右面的那一块区域消失不见了,

这是因为每一个TextView 都是android:layout_width=”match_parent”

这时每一个区域都是填充整个的布局,因此通过需要进行计算,

第一步:由于每一块区域都是match_parent,但是屏幕只有一个,所以有1-3=-2match_parent

第二步:他们的比例为1/6. 2/6 .3/6,

第三步:左面:1-2*(1/6)=2/3match_parent

中间 :1-2*(2/6)=1/3match_parent

右面: 1-2*(3/6)=0match_parent

所以,右面区域占比例为0,不显示

这时将左面改为android:layout_weight=”2” 时,就可以显示出右面,如下:



LinearLayout的分割线属性

第一种:自己设置:

如下代码:

<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#000000"/>


第二种:

1.divider属性:设置作为分隔的图片

2.showDividers属性:none(无)beginning(开始)end(结束)middle(两个组件之间)

3.dividerPadding属性:设置分割线的Padding。

总结:

LinearLayout是android中的基本布局之一,LinearLayout还有许多其他的属性和用法,这里就不一一举例,需要在根据使用场景在实际开发中去发现。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  android