当前位置: 首页 > backend >正文

LinearLayout

LinearLayout,其实就是线性布局,结构层次相对来说简单非常明了,只有横和竖2条直线的方向。这里主要记录下LinearLayout的重要属性,以及使用时需要注意的事项。

1.排列方式orientation

在XML布局中:

android:orientation="vertical"//垂直排列

android:orientation="horizontal"//水平排列

注意事项:

控制LinearLayout的子控件内部具体的排列顺序,还需要使用android:layout_gravity这个属性。

可是当orientation为vertical时,子控件设置android:layout_gravity="center_vertical"或者自身设置android:gravity="center_vertical"是无效的。

同样当orientation为horizontal时,子控件设置android:layout_gravity="center_horizontal"或者自身设置android:gravity="center_horizontal"是无效的。

2.摆放位置gravity

从上面可以得知:在父控件中android:gravity属性和其子控件的android:layout_gravity效果是一样的。

既然上面提到了gravity,下面就对它的选项(可以多选)进行下解释:

center:居中  center_horizontal:水平居中 center_vertical:垂直居中

left:偏左  |right:偏右  (start和end:详见 这里有这2个属性的解释)

bottom:偏下 |top:偏上

下面的一些没怎么用过:

fill:充满容器 |fill_horizontal:水平方向充满容器 |fill_vertical:垂直方向充满容器

clip_horizontal:水平裁剪|clip_vertical:垂直裁剪

3.分割线showDividers

 

既然是设置分割线,首先我们就需要有分割线:

android:divider="@drawable/drawable"//分割线的drwable,不能直接给color(无效)

android:dividerPadding="0.5dp"//分割线高度或者宽度

分割线的Shape.xml:

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

<solid android:color="@color/colorAccent"/>

<size android:height="1px"/>

</shape>

注意:size必须设置,不然显示不出

选择的样式(可以多选):

LinearLayout.SHOW_DIVIDER_BEGINNING;//开始的分割线

LinearLayout.SHOW_DIVIDER_MIDDLE;//中间的分割线

LinearLayout.SHOW_DIVIDER_END;//结束的分割线

LinearLayout.SHOW_DIVIDER_NONE;//没有分割线

其他方法:

用一个View将高度或宽度设置为match_parent,另一个长度设置为0.5dp,设置一个background

来达到分割线的效果:

<View android:layout_width="match_parent"

android:layout_height="0.5dp"

android:background="@color/colorPrimary"/>

在开发中,用的这种方式,可是个人觉得上面这个方式,代码比较简约.

4.基线baselineAligned

 

上图中2个布局其实都差不多,唯一的区别就是:第二个布局的android:baselineAligned="false";

不难发现有基线的情况下,文字默认都在一条直线上,这样我们有时候就会有布局的麻烦,解决方法当然就是设置基线为false;

同时,我们可以利用基线来布局如布局二的样式,是不是感觉很常见的一个底部菜单栏样式?

注意:基线只对有文字内容,如TextView,EditText,Button等才有效果。

5.权重weight

 

图一的布局:

<LinearLayout

android:layout_width="match_parent"

android:layout_height="50dp"

android:orientation="horizontal">

<TextVIew android:id="@+id/tv1"

android:layout_width="wrap_content"

android:layout_height="50dp"

android:layout_weight="1"

android:background="@color/colorPrimary"/>

<TextView android:id="@+id/tv2"

android:layout_width="wrap_content"

android:layout_height="50dp"

android:layout_weight="2"

android:background="@color/colorAccent"/>

<TextView android:id="@+id/tv3"

android:layout_width="wrap_content"

android:layout_height="50dp"

android:layout_weight="2"

android:background="@color/colorPrimaryDark"/>

</LinearLayout>

图二的布局:

仅仅只是将TextView的android:layout_width="match_parent";

在图片可以看出,wrap_content情况下, tv1:tv2:tv3=1:2:2;刚好是权重的比值。

符合大家所说的权重比值,权重越多比例越大。

可是在match_parent的情况下,tv1:tv2:tv3=3:1:1,这样明显不是我们想要的结果。

那么权重到底是怎么计算的呢?

首先按照分配的长度来给予长度,剩余的长度再按权重的比例来分配。

图二 tv1的宽度:match_parent,我们这里用L表示。

那么tv2和tv3的宽度也是L。

这时候剩余的长度呢?当然是总长度减去3个控件的长度,即:L-3L=-2L。

然后将这个剩余长度再来分配:

tv1的宽度=L-(1/5)2L=(3/5)L;

tv2和tv3的宽度=L-(2/5)2L=(1/5)L;

6.源码地址

LinearLayoutDemo

http://www.xdnf.cn/news/11436.html

相关文章:

  • 29 | 分布式高可靠之流量控制:大禹治水,在疏不在堵
  • 【Android OpenGL开发】OpenGL ES与EGL介绍
  • ContentProvider的相关知识总结
  • 新加坡云服务器推荐 - 适合跨境外贸等业务
  • 2 v11补丁安装_老款macbook机型欺骗补丁强制安装macOS11 Big Sur图文详解
  • Linux下权限的修改-JDK的配置-文件的常见操作
  • Java学习 布局管理器之GridLayout(网格布局)
  • 企业最新几种好用的数据同步工具对比
  • Qt 多线程的几种实现方式
  • Linux系统三步安装QQ
  • C/C++网络编程
  • JSTL-核心标签库
  • 动态域名内网穿透(永久免费)
  • 设计模式七大原则-迪米特法原则
  • 安全小课堂丨什么是暴力破解?如何防止暴力破解
  • 谷歌浏览器、Yandex浏览器使用体验分享
  • 光流法(optical flow)简介
  • 13800138000来电?手机管家:小心诈骗
  • Windows 10 离线安装 .NET Framework 3.5 的方法和技巧
  • WISP模式
  • waterdrop介绍
  • CreateThread()函数及_beginthreadex()函数
  • C++ ofstream和ifstream详细用法
  • 使用Latex制作分享,演讲,Presentation用的Slides,PPT——Beamer教程
  • .htaccess语法讲解
  • [Linux系列]Chrony时间同步服务器
  • 下载了鲁大师,打开goolgle浏览器弹出360导航页
  • Web前端开发【新手入门指南】
  • JFreeChart 使用介绍
  • maven 环境变量的配置