Android布局(一)--- LinearLayout(线性布局)

当android:orientation="vertical"时,子View只有水平方向的设置生效,当android:orientation="horizontal"时,子View只有垂直方向的设置生效。父元素设置的gravity优先级要低于子元素设置的layout_gravity。

1、线性布局中的元素属性

android:gravity 表示该组件的子元素的对齐方式

android:layout_gravity 表示该组件在其父元素内的对其方式

android:layout_width 表示该组件宽度

android:layout_height 表示该组件高度

android:id 表示该组件资源id

android:background 表示该组件背景

android:weight 表示该组件的权重

android:layout_margin 外边距

android:layout_padding 内边距


2、weight(权重)属性详解:

LinearLayout有一个权重数量的标记:weightSum,在LinearLayout中没有声明weightSum时,默认的就是各个控件权重的总和

①最简单用法:

如图:

实现代码:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"   

    xmlns:tools="http://schemas.android.com/tools"   

    android:id="@+id/LinearLayout1"   

    android:layout_width="match_parent"   

    android:layout_height="match_parent"   

    android:orientation="horizontal"> 

    <LinearLayout   

        android:layout_width="0dp"   

        android:layout_height="fill_parent"   

        android:background="#ADFF2F"   

        android:layout_weight="1"/>   

    <LinearLayout   

        android:layout_width="0dp"   

        android:layout_height="fill_parent"   

        android:background="#DA70D6"   

        android:layout_weight="2"/>   

        </LinearLayout> 

要实现第一个的1:1的效果,只需要分别把两个LinearLayout的weight改成1和1就可以了。 按比例划分水平方向:将涉及到的View的android:width属性设置为0dp,然后设置为android weight属性设置比例即可,如果是竖直方向,只需设android:height为0dp,然后设weight属性即可。

②weight属性详解:

当然,如果我们不适用上述那种设置为0dp的方式,直接用wrap_content和match_parent的话,则要接着解析weight属性了,分为两种情况:wrap_content与match_parent。另外还要看 LinearLayout的orientation是水平还是竖直,这个决定哪个方向等比例划分。

1)wrap_content比较简单,直接就按比例的了

实现代码:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"   

    xmlns:tools="http://schemas.android.com/tools"   

    android:id="@+id/LinearLayout1"   

    android:layout_width="match_parent"   

    android:layout_height="match_parent" 

    android:orientation="horizontal" >   

    <TextView   

        android:layout_weight="1"   

        android:layout_width="wrap_content"   

        android:layout_height="fill_parent"   

        android:text="one"   

        android:background="#98FB98"   

    />   

    <TextView   

        android:layout_weight="2"   

        android:layout_width="wrap_content"   

        android:layout_height="fill_parent"   

        android:text="two"   

        android:background="#FFFF00"   

    />   

    <TextView   

        android:layout_weight="3"   

        android:layout_width="wrap_content"   

        android:layout_height="fill_parent"   

        android:text="three"   

        android:background="#FF00FF"   

    />   

    </LinearLayout> 

2)match_parent(fill_parent):这个则需要计算了

我们写这段简单的代码:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"   

    xmlns:tools="http://schemas.android.com/tools"   

    android:id="@+id/LinearLayout1"   

    android:layout_width="match_parent"   

    android:layout_height="match_parent" >   

    <TextView   

        android:layout_weight="1"   

        android:layout_width="fill_parent"   

        android:layout_height="fill_parent"   

        android:text="one"   

        android:background="#98FB98"   

    />   

    <TextView   

        android:layout_weight="2"   

        android:layout_width="fill_parent"   

        android:layout_height="fill_parent"   

        android:text="two"   

        android:background="#FFFF00"   

    />   

    <TextView   

        android:layout_weight="3"   

        android:layout_width="fill_parent"   

        android:layout_height="fill_parent"   

        android:text="three"   

        android:background="#FF00FF"   

    />   

    </LinearLayout>

运行效果图:

系统先给第1个子控件分配parent_width(剩余空间),再给第2个分配parent_width,再给第3个分配parent_width,即分配了3个parent_width,此时剩余空间为 parent_width - 3parent_width = -2parent_width。接着,第一个控件占宽度:parent_width(当前已有宽度)+ 权重1/6*(-2parent_width) = 2/3parent_width;第二个控件占宽度:parent_width + 权重1/3*(-2parent_width) = 1/3parent_width;第三个控件占宽度:parent_width + 权重1/2*(-2parent_width) = 0parent_width。得出公式:C表示子布局声明的大小,B表示剩余布局的大小,P表示子布局占据父布局剩余布局的比例,则子布局最终的实际大小R为:R = C + B * P。

0dp与wrap_content

谷歌官方建议使用0dp,来分比例显示布局,和wrap_content大同小异,当使用layout_weight时,都表示占据剩余宽度或高度的比重。

但两者有明显区别。

使用0dp时,要考虑所分配的布局宽度是否小于控件实际宽度,比如:

<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"android:id="@+id/linear_layout"android:layout_width="match_parent"android:layout_height="match_parent"><TextViewandroid:layout_width="0dp"android:layout_height="wrap_content"android:layout_weight="1"android:background="@android:color/holo_blue_dark"android:padding="5dp"android:text="left"/><TextViewandroid:layout_width="0dp"android:layout_height="wrap_content"android:layout_weight="19"android:background="@android:color/holo_green_dark"android:gravity="center"android:padding="5dp"android:text="right"/></LinearLayout>

则显示为如下:

而使用wrap_content则不会出现上面的那种情况

<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"android:id="@+id/linear_layout"android:layout_width="match_parent"android:layout_height="match_parent"><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_weight="1"android:background="@android:color/holo_blue_dark"android:padding="5dp"android:text="left"/><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_weight="19"android:background="@android:color/holo_green_dark"android:gravity="center"android:padding="5dp"android:text="right"/></LinearLayout>

这里left先获取自适应的大小,然后再去获取剩余布局的1/20。

0dp与match_parent

可以发现match_parent是与0dp的效果是相反的

<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"android:id="@+id/linear_layout"android:layout_width="match_parent"android:layout_height="match_parent"><TextViewandroid:layout_width="match_parent"android:layout_height="wrap_content"android:layout_weight="1"android:background="@android:color/holo_blue_dark"android:padding="5dp"android:text="left"/><TextViewandroid:layout_width="match_parent"android:layout_height="wrap_content"android:layout_weight="2"android:background="@android:color/holo_green_dark"android:gravity="center"android:padding="5dp"android:text="right"/></LinearLayout>

权重计算:
控件的实际大小 = 控件设定的大小 + (布局剩余的大小) * 布局的比例

布局剩余的大小 = 父布局的大小 - 子布局大小之和


3、LinearLayout 设置分割线

(1)直接在布局中添加一个view,作用仅仅是显示出一条线

<View  

    android:layout_width="match_parent"  

    android:layout_height="1px"  

    android:background="#000000" />

(2)使用LinearLayout的divider属性

第一步准备一张分割线的图片;第二步 android:divider 设置作为分割线的图片;第三步 android:showDividers 设置分割线的位置,none(无)、begining(开始)、end(结束)、middle(每两个组件之间);第四步 android:dividerPadding 设置分割线的Padding。

android:divider="@drawable/cut_off_rule"

android:showDividers="middle"

android:dividerPadding="5dp"

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 160,706评论 4 366
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 68,002评论 1 301
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 110,462评论 0 250
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 44,375评论 0 216
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,763评论 3 294
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,849评论 1 224
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 32,033评论 2 317
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,768评论 0 204
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,490评论 1 246
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,734评论 2 253
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,204评论 1 264
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,566评论 3 260
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,227评论 3 241
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,137评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,934评论 0 201
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,926评论 2 283
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,774评论 2 274

推荐阅读更多精彩内容