写在前面的话
<p>
LinearLayout对于开发来说,是使用最常用的布局控件之一,但是对于LinearLayout我们究竟有多了解呢?最近在看LinearLayout的源码,看源码过程中发现其实有很多东西自己并没有使用到,对于LinearLayout的了解也是并没有那么足,那么这篇文章就对LinearLayout进行更加详细与深入的了解,从使用到源码,一一进行解析!
一.LinearLayout属性
<p>
1)基准线对齐
<p>
xml属性 : android:baselineAligned;
设置方法 : setBaselineAligned(boolean b);
作用 : 如果该属性为false, 就会阻止该布局管理器与其子元素的基准线对齐;
要知道这个属性是干嘛的首先要知道什么是基准线,如下图
图1 基准线1.基准点是baseline
2.ascent:是baseline之上至字符最高处的距离
3.descent:是baseline之下至字符最低处的距离
4.leading:是上一行字符的descent到下一行的ascent之间的距离,也就是相邻行间的空白距离
5.top:是指的是最高字符到baseline的值,即ascent的最大值
6.bottom:是指最低字符到baseline的值,即descent的最大值
其实基准线对于对自定义View有一定了解的小伙伴都会比较熟悉
所以设置基准线对齐有什么区别呢?
上代码
<?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"
android:baselineAligned="false"
>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="JSON" />
<Button
android:layout_width="wrap_content"
android:layout_height="100dp"
android:text="Hello World" />
</LinearLayout>
分别设置android:baselineAligned 为true 与false
运行如图
2)基准线对齐对象
<p>
xml属性 : android:baselineAlignedChildIndex;
设置方法 : setBaselineAlignedChildIndex(int i);
作用 : 设置文字基线对齐的子控件;
基准线对其上面有介绍过,那么这个基准线对齐对象其实就是设置文字基线对齐的子控件
接下来直接上代码
<?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="wrap_content"
android:orientation="horizontal"
>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#1E9066"
android:layout_weight="1"
android:baselineAlignedChildIndex="0"
android:orientation="horizontal" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="zero"
android:textSize="5dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="One"
android:textSize="30dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TWo"
android:textSize="15dp" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#CFCFCF"
android:layout_weight="1"
android:baselineAlignedChildIndex="1"
android:orientation="horizontal" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="zero"
android:textSize="5dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="One"
android:textSize="30dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TWO"
android:textSize="15dp" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#1E90FF"
android:layout_weight="1"
android:baselineAlignedChildIndex="2"
android:orientation="horizontal" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="zero"
android:textSize="5dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="One"
android:textSize="30dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TWo"
android:textSize="15dp" />
</LinearLayout>
</LinearLayout>
三个相同的布局分别设置基准线为第一个,第二个和第三个控件
运行如图
图4 baselineAlignedChildIndex3)设分隔条
<p>
xml属性 : android:divider="@drawable/shape"
android:showDividers="middle|beginning|end"
设置方法 : setDividerDrawable(Drawable);
setShowDividers(int showDividers)
作用 : 设置布局中两个按钮之间的分隔条;
分割线如果是图片那就直接使用图片就行,如果要使用颜色就必须使用shape来显示,直接使用颜色或Color是没有用的 使用shape的时候要注意设置size属性不设置宽高分割线就不会显示出来,如果使用line那填充颜色只能使用stroke来显示颜色
space_divider.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="@color/colorAccent" />
<size android:height="1px" />
</shape>
<?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="wrap_content"
android:orientation="vertical"
android:divider="@drawable/space_divider"
android:showDividers="middle"
>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="JSON" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Hello World" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Hello " />
</LinearLayout>
运行如下
图5 setShowDividers设置为middle 图6 setShowDividers设置为end与beginning4)权重最小尺寸
<p>
xml属性 : android:measureWithLargestChild;
设置方法 : setMeasureWithLargestChildEnable(boolean b);
作用 : 该属性为true的时候, 所有带权重的子元素都会具有最大子元素的最小尺寸;
代码如下
<?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="wrap_content"
android:orientation="vertical"
>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:measureWithLargestChild="true"
>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="JSON" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Hello Wor" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="He " />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="JSON" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Hello World" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Hello " />
</LinearLayout>
</LinearLayout>
运行如图
图7 权重最小尺寸设置为true可以看出设置与没有设置的区别还是很大的,当我们设置权重最小尺寸时,系统会把最大控件的最小尺寸作为其他子控件的尺寸,所以看到图中上半部分的控件的大小其实都是一样的
5)设置权重总和
<p>
xml属性 : android:weightSum;
设置方法 : setWeightSum(float weightSum);
作用 : 设置权重的总和。(默认是全部子控件权重之和);
定义weight总和的最大值。如果未指定该值,以所有子视图的layout_weight属性的累加值作为总和的最大值。
这个权重总和可能大家觉得并不是很有用,但是对于有些场景很有用,比如我们需要一个布局在总布局的3/4的位置
如下所示
图8 例子图如果不使用权重总和这个属性的话,实现起来就只能用动态布局通过屏幕的尺寸来重新布局子View了
而使用权重总和就可以很优雅的解决这个问题了
代码如下
<?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"
android:weightSum="1"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:background="@color/colorAccent"
android:layout_weight="0.75"
android:measureWithLargestChild="true"
>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="JSON" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Hello Wor" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="He " />
</LinearLayout>
</LinearLayout>
这里其实就是设置父Layout的权重总和为1,子layout的权重为3/4
运行如下
图9 权重总和6)对齐方式(控制内部子元素)
<p>
xml属性 : android:gravity;
设置方法 : setGravity(int);
作用 : 设置布局管理器内组件(子元素)的对齐方式,
支持的属性 :
-
top, bottom, left, right,
-
center_vertical(垂直方向居中), center_horizontal(水平方向居中),
-
fill_vertical(垂直方向拉伸), fill_horizontal(水平方向拉伸),
-
center, fill,
-
clip_vertical, clip_horizontal;
-
可以同时指定多种对齐方式 : 如 left|center_vertical 左侧垂直居中;
关于对齐方式就不做详细的介绍了,因为我相信大家都很了解
7)排列方式
<p>
xml属性 : android:orientation;
设置方法 : setOrientation(int i);
作用 : 设置布局管理器内组件排列方式, 设置为horizontal(水平),vertical(垂直), 默认为垂直排列;
这个大家应该更了解,也不做介绍了
写在后面的几句话
<p>
本来想把LinearLayout的源码在一起进行分析,但是考虑到篇幅确实够长,所以分为两篇进行说明,下一篇会对LinearLayout的源码进行分析。。。。
网友评论