初识安卓之LinearLayout线性布局

0.前言

在安卓的多种布局方式中,LinearLayout(线性布局)RelativeLayout(相对布局)算是最为常用的两种方式了,这里只讲解线性布局,随后将会更新相对布局的相关知识。

1.常用属性

下面列出几种之后可能会用到LinearLayout中的属性,遗忘时查询即可。

属性名 意义
orientation 布局方式,有horizontal(水平布局)vertical(垂直布局)两种方式
id 组件名称,方便之后调用
layout_width 该组件的宽度
layout_height 该组件的高度
layout_weight 权重
layout_gravity 该组件(在父容器)中的对齐方式
gravity 该组件所含子组件在其内部的对齐方式
background 设置背景图片或填充颜色

2.orientation属性示例

orientation属性有horizontalvertical两个参数供选择,参数的值决定了布局方式。

①水平布局(horizontal)


代码如下:

 <LinearLayout
        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:gravity="center"
            android:text="第一个文本框"
            android:background="#ffff00"/>
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:gravity="center"
            android:text="第二个文本框"
            android:background="#ff0000"/>
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:gravity="center"
            android:text="第三个文本框"
            android:background="#008000"/>
    </LinearLayout>

②垂直布局(vertical)


代码如下:

   <LinearLayout
       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:gravity="center"
           android:text="第一个文本框"
           android:background="#ffff00"/>
       <TextView
           android:layout_width="match_parent"
           android:layout_height="wrap_content"
           android:gravity="center"
           android:text="第二个文本框"
           android:background="#ff0000"/>
       <TextView
           android:layout_width="match_parent"
           android:layout_height="wrap_content"
           android:gravity="center"
           android:text="第三个文本框"
           android:background="#008000"/>
   </LinearLayout>

3.layout_weight属性示例

在这之前先简要说明下match_parentfill_parentwrap_content
match_parentfill_parent在效果上相同,都是使该组件的宽度或高度填满父元素,也就是说令其宽度或高度与父元素的宽度或高度一致
wrap_content产生的效果由该组件本身的实际情况决定,例如一个TextView组件,它的宽度就是由文本实际的内容多少决定的。
回到正题,weight,这里理解为权重,或是比例,需要注意的是:
水平布局时,按比例分配父元素的宽度(width)
竖直布局时,按比例分配父元素的高度(height)
先看一个例子(以水平布局举例):

    <LinearLayout
        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:gravity="center"
            android:text="第一个文本框"
            android:layout_weight="1"
            android:background="#ffff00"/>
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:gravity="center"
            android:text="第二个文本框"
            android:layout_weight="1"
            android:background="#ff0000"/>
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:gravity="center"
            android:text="第三个文本框"
            android:layout_weight="1"
            android:background="#008000"/>
    </LinearLayout>

还是之前水平布局的例子,只是我们给每个TextView添加了一句android:layout_weight="1"


貌似达到了我们的预期,三个TextView按1:1:1分完了总宽度。
但有时候可能无法达到预期的效果,请看下面的例子,将三个TextViewandroid:layout_weight依次设置为'1','2','3':

可以看出三个文本框的大小不一致了,但是明显没有达到我们预期的1:2:3的效果。
这是因为我们设置的每个TextView的宽度为wrap_content,每个组件都有本身由text内容决定的宽度,只是将剩余的在宽度上的空间按比例分好后再加在其本身的宽度上。

所以说,为达到真正的比例效果:
水平布局时,将每个组件的宽度(width)设置为0dp
竖直布局时,将每个组件的高度(height)设置为0dp

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="horizontal">
        <TextView
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:gravity="center"
            android:text="第一个文本框"
            android:layout_weight="1"
            android:background="#ffff00"/>
        <TextView
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:gravity="center"
            android:text="第二个文本框"
            android:layout_weight="2"
            android:background="#ff0000"/>
        <TextView
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:gravity="center"
            android:text="第三个文本框"
            android:layout_weight="3"
            android:background="#008000"/>
    </LinearLayout>


这样的用法用得较多,那么如果再将每个组件的宽度改为match_parent呢,效果如下:

第一个文本框明显比第二个大,而且第三个文本框消失了,像这样的水平布局中且将所有孩子组件的宽度设置为了match_parent,还要使用layout_weight属性,要得知真正的比例,便需要计算了,下面介绍一种计算方式:
①用1减去孩子组件的数目(也就是分成的区域数),得到数m。
②计算每个孩子组件的layout_weight对应值占所有layout_weight和的比例n。
③用1+m×n计算出的结果即为实际比例。
例如上面那个例子:
m = 1 - 3 = -2,每个组件的layout_weight所占比例依次为1/6,1/3,1/2,根据公式计算实际比例:
第一个文本框:1-2×1/6 = 2/3
第二个文本框:1-2×1/3 = 1/3
第三个文本框:1-2×1/2 = 0
与实际结果一致,多试试就熟练了。

如有错误,欢迎指正~

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 问答题47 /72 常见浏览器兼容性问题与解决方案? 参考答案 (1)浏览器兼容问题一:不同浏览器的标签默认的外补...
    _Yfling阅读 13,796评论 1 92
  • 官方网站:http://www.skinui.cn 下载地址:http://pan.baidu.com/s/1sl...
    吴忠亮阅读 1,100评论 0 1
  • 一、几种基本布局 1、线性布局 LinearLayout又称为线性布局,是一种非常常用的布局。这个布局会将它所包含...
    zda123000阅读 10,499评论 0 8
  • 前言 在进行Android开发中,常常需要用到各种布局来进行UI的绘制,今天我们就来讲下Android开发中最常用...
    残月雨纷纷阅读 641评论 0 6
  • 文/雍万萍 巾帼不让须眉男 莫因女儿视等闲 军中藏花十二载 传奇一生记木兰
    频彩阅读 233评论 0 7