天天看点

3.2.1 LinearLayout(线性布局)3.2.1 LinearLayout(线性布局)

3.2.1 LinearLayout(线性布局)

标签: StudyNote

本文声明:

本文由Coder-pig编写,想了解其他内容,可见CoderPig’s Android Study Note——目录

尊重作者劳动成果,未经本人授权,禁止转载!违者必究!

目录源地址:http://blog.csdn.net/coder_pig/article/details/51348769

1.基本属性

  • orientation:设置布局中组件的排放方式,horizontal(水平),vertical(垂直)
  • gravity:控制子元素的对齐方式,可多个组合(left|buttom)
  • layout_gravity:控制改组件在父容器中的对齐方式

2.weight(权重)属性详解

核心公式:

View的宽/高 = 原有宽度(android:layout_width) + 剩余空间的占比!

①推荐用法:把widht/height设置为0dp

按比例划分水平方向:将涉及到的View的android:width属性设置为0dp,

然后设置为android weight属性设置比例即可;类推,竖直方向,只需设

android:height为0dp,然后设weight属性即可! 比如:

<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="0dp"
        android:layout_height="48dp"
        android:background="#03A9F4"
        android:layout_weight="1"
        android:text="11111111111"/>

    <TextView
        android:layout_width="0dp"
        android:layout_height="48dp"
        android:background="#FFEB3B"
        android:layout_weight="2"
        android:text="B"/>

    <TextView
        android:layout_width="0dp"
        android:layout_height="48dp"
        android:background="#4CAF50"
        android:layout_weight="3"
        android:text="C"/>

</LinearLayout>
           
3.2.1 LinearLayout(线性布局)3.2.1 LinearLayout(线性布局)

咦,文字左对齐好像不怎么好看,我们为每个TextView加一个android:gravity=”center”,

刚加一个就发现:

3.2.1 LinearLayout(线性布局)3.2.1 LinearLayout(线性布局)

卧槽,错位了,什么鬼,我们为剩下两个TextView也加上:android:gravity=”center”,

3.2.1 LinearLayout(线性布局)3.2.1 LinearLayout(线性布局)

好吧,其实引起这个原因是baselineAligned属性引起的,默认为true,就是让布局控件

中子控件内的文字的基线对齐,所以会出现这个错位的问题,只要在外层的LinearLayout

中将这个属性设置为false即可解决这个问题。

②不建议用法:wrap_content

这个就不怎么好用了,先分配能够显示包含他们的内容的宽/高,然后布局容器的宽/高 减去

去这几个的宽/高和求出剩余空间的宽/高,再按比例分配给每个View。

3.2.1 LinearLayout(线性布局)3.2.1 LinearLayout(线性布局)
3.2.1 LinearLayout(线性布局)3.2.1 LinearLayout(线性布局)
3.2.1 LinearLayout(线性布局)3.2.1 LinearLayout(线性布局)
3.2.1 LinearLayout(线性布局)3.2.1 LinearLayout(线性布局)

③match_parent

这个稍微好算一点,都设置成match_parent,weight设置为:1:2:3的,结果是这样的

3.2.1 LinearLayout(线性布局)3.2.1 LinearLayout(线性布局)

换成1:2:2呢?

3.2.1 LinearLayout(线性布局)3.2.1 LinearLayout(线性布局)

接着我们来解释下为什么会出现这个结果,先说下1:2:3的

1:三个初始宽都是macth_parent,那么剩余的空间 = (1 - 3) * macth_parent = -2 * macth_parent

2:接着把剩余的空间-2 * macth_parent分成6份(1+2+3),接着按比例分配:、

A的宽度 = macth_parent + (-2 * macth_parent /6 * 1) = 2/3 macth_parent;

B的宽度 = macth_parent + (-2 * macth_parent /6 * 2) = 1/3 macth_parent;

C的宽度 = macth_parent + (-2 * macth_parent /6 * 3) = 0 macth_parent;

非常简单,你可以按照上面的计算方法接着试下1:2:2,得到的结果会是:3:1:1.

3.weightSum设置子控件weight之和

可以巧妙的用这个东西来实现留白,效果见示例代码:

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

    <TextView
        android:layout_width="0dp"
        android:layout_height="48dp"
        android:layout_weight="1"
        android:background="#03A9F4"
        android:gravity="center"
        android:text="A" />

    <TextView
        android:layout_width="0dp"
        android:layout_height="48dp"
        android:layout_weight="1"
        android:background="#FFEB3B"
        android:gravity="center"
        android:text="B" />

</LinearLayout>
           
3.2.1 LinearLayout(线性布局)3.2.1 LinearLayout(线性布局)

4.为LinearLayout设置分割线

方法有两种,第一种设置一个1px的View,第二种设置LinearLayout的divider相关的属性

divider相关属性:

  • divider设置作为分割线的图片,一般我们都会自己写一个shape来作为分割线。
  • showDividers设置分割线的位置,none(无),begining(开始),end(结束),middle(每两个组件间)
  • dividerPadding设置分割线的Padding

shape分割线模板:

<?xml version="1.0" encoding="utf-8"?>
 <shape xmlns:android="http://schemas.android.com/apk/res/android"
         android:shape="rectangle" >
     <size
         android:height="48dp"
         android:width="1dp" />
     <solid android:color="#000000" /> -->
 </shape>