转载自:http://blog.csdn.net/coder_pig/article/details/42344615

 ——转载请注明出处:coder-pig,欢迎转载,请勿用于商业用途!


小猪Android开发交流群已建立,欢迎大家加入,无论是新手,菜鸟,大神都可以,小猪一个人的

力量毕竟是有限的,写出来的东西肯定会有很多纰漏不足,欢迎大家指出,集思广益,让小猪的博文

更加的详尽,帮到更多的人,O(∩_∩)O谢谢!

小猪Android开发交流群:小猪Android开发交流群群号:421858269

新Android UI实例大全目录:http://blog.csdn.net/coder_pig/article/details/42145907

本节引言:


Android中的布局分为六大布局,分别是:

LinearLayout(线性布局),RelativeLayout(相对布局),TableLayout(表格布局)

FrameLayout(帧布局),AbsoluteLayout(绝对布局),GridLayout(网格布局)

而今天我们要讲解的就是第一个布局,LinearLayout(线性布局),我们屏幕适配的使用

用的比较多的就是LinearLayout的weight(权重属性),在这一节里,我们会详细地解析

下LinearLayout,包括一些基本的属性,Weight属性的使用,以及比例如何计算,另外还

会说下一个用的比较少的属性:android:divider绘制下划线



本节学习图:




2.weight(权重)属性

①最简单用法:

如图:


实现代码:

[html] view plain copy print ?
  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2. xmlns:tools="http://schemas.android.com/tools"
  3. android:id="@+id/LinearLayout1"
  4. android:layout_width="match_parent"
  5. android:layout_height="match_parent"
  6. android:orientation="horizontal"
  7. >
  8. <LinearLayout
  9. android:layout_width="0dp"
  10. android:layout_height="fill_parent"
  11. android:background="#ADFF2F"
  12. android:layout_weight="1"/>
  13. <LinearLayout
  14. android:layout_width="0dp"
  15. android:layout_height="fill_parent"
  16. android:background="#DA70D6"
  17. android:layout_weight="2"/>
  18. </LinearLayout>
<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比较简单,直接就按比例的了


实现代码:

[html] view plain copy print ?
  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2. xmlns:tools="http://schemas.android.com/tools"
  3. android:id="@+id/LinearLayout1"
  4. android:layout_width="match_parent"
  5. android:layout_height="match_parent"
  6. android:orientation="horizontal" >
  7. <TextView
  8. android:layout_weight="1"
  9. android:layout_width="wrap_content"
  10. android:layout_height="fill_parent"
  11. android:text="one"
  12. android:background="#98FB98"
  13. />
  14. <TextView
  15. android:layout_weight="2"
  16. android:layout_width="wrap_content"
  17. android:layout_height="fill_parent"
  18. android:text="two"
  19. android:background="#FFFF00"
  20. />
  21. <TextView
  22. android:layout_weight="3"
  23. android:layout_width="wrap_content"
  24. android:layout_height="fill_parent"
  25. android:text="three"
  26. android:background="#FF00FF"
  27. />
  28. </LinearLayout>
<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):这个责需要计算了

我们写这段简单的代码:

[html] view plain copy print ?
  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2. xmlns:tools="http://schemas.android.com/tools"
  3. android:id="@+id/LinearLayout1"
  4. android:layout_width="match_parent"
  5. android:layout_height="match_parent" >
  6. <TextView
  7. android:layout_weight="1"
  8. android:layout_width="fill_parent"
  9. android:layout_height="fill_parent"
  10. android:text="one"
  11. android:background="#98FB98"
  12. />
  13. <TextView
  14. android:layout_weight="2"
  15. android:layout_width="fill_parent"
  16. android:layout_height="fill_parent"
  17. android:text="two"
  18. android:background="#FFFF00"
  19. />
  20. <TextView
  21. android:layout_weight="3"
  22. android:layout_width="fill_parent"
  23. android:layout_height="fill_parent"
  24. android:text="three"
  25. android:background="#FF00FF"
  26. />
  27. </LinearLayout>
<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> 

运行效果图:


这个时候就会有疑问了,怎么会这样,这比例是2:1吧,那么three去哪了?代码里面明明有

three的啊,还设置了3的,而1和2的比例也不对耶,1:2:3却变成了2:1:0,怎么会这样呢?

答:这里其实没那么简单的,还是需要我们计算的,网上给出的算法有几种,这里就给出笔者

觉得比较容易理解的一种:

step 1:个个都是fill_parent,但是屏幕只有一个啦,那么1 - 3 = - 2 fill_parent
step 2:依次比例是1/6,2/6,3/6
step 3:先到先得,先分给one,计算: 1 - 2 * (1/6) = 2/3 fill_parent
           接着到two,计算: 1 - 2 * (2/6) = 1/3 fill_parent
           最后到three,计算 1 - 2 * (3/6) = 0 fill_parent

step 4:所以最后的结果是:one占了两份,two占了一份,three什么都木有

以上就是为什么three没有出现的原因了,或许大家看完还是有点蒙,没事,我们举多几个

例子就知道了:

比例为:1:1:1


按照上面的计算方法算一次,结果是:1/3   1/3   1/3,没错

接着我们再试下:2:3:4


计算结果:5/9   3/9   1/9,对比效果图,5:3:1,也没错,所以这个计算方法你可得mark下了!


③Java代码设置weight属性:

[java] view plain copy print ?
  1. setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
  2. LayoutParams.WRAP_CONTENT, 1));
setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT, 1));  


3.为LinearLayout设置分割线

很多界面开发中都会设置一些下划线,或者分割线,从而使得界面更加整洁美观,比如下面的酷狗

音乐的注册页面:


对于这种线,我们通常的做法有两种

①直接在布局中添加一个view,这个view的作用仅仅是显示出一条线,代码也很简单:

[html] view plain copy print ?
  1. <View
  2. android:layout_width="match_parent"
  3. android:layout_height="1px"
  4. android:background="#000000" />
<View
android:layout_width="match_parent"
android:layout_height="1px"
android:background="#000000" />

这个是水平方向上的黑线,当然你也可以改成其他颜色,或者使用图片

②第二种则是使用LinearLayout的一个divider属性,直接为LinearLayout设置分割线

这里就需要你自己准备一张 线的图片了

1)android:divider设置作为分割线的图片

2)android:showDividers设置分割线的位置,none(无),begining(开始),end(结束),middle(每两个组件间)

3)dividerPadding设置分割线的Padding

使用示例:


实现代码:

[html] view plain copy print ?
  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2. xmlns:tools="http://schemas.android.com/tools"
  3. android:id="@+id/LinearLayout1"
  4. android:layout_width="match_parent"
  5. android:layout_height="match_parent"
  6. android:divider="@drawable/ktv_line_div"
  7. android:orientation="vertical"
  8. android:showDividers="middle"
  9. android:dividerPadding="10dp"
  10. tools:context="com.jay.example.linearlayoutdemo.MainActivity" >
  11. <Button
  12. android:layout_width="wrap_content"
  13. android:layout_height="wrap_content"
  14. android:text="按钮1" />
  15. <Button
  16. android:layout_width="wrap_content"
  17. android:layout_height="wrap_content"
  18. android:text="按钮2" />
  19. <Button
  20. android:layout_width="wrap_content"
  21. android:layout_height="wrap_content"
  22. android:text="按钮3" />
  23. </LinearLayout>
<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:divider="@drawable/ktv_line_div"
android:orientation="vertical"
android:showDividers="middle"
android:dividerPadding="10dp"
tools:context="com.jay.example.linearlayoutdemo.MainActivity" >
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="按钮1" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="按钮2" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="按钮3" />
</LinearLayout>

上述代码中我们设置了分割线的图片,以及设置分割线的出现位置,以及设置了padding,就实现了上面的效果!

当然我们也可以在代码中setXxx或者getXxx设置或者获得这些属性的值



4.LinearLayout的简单例子:


实现代码如下:

[html] view plain copy print ?
  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2. xmlns:tools="http://schemas.android.com/tools"
  3. android:id="@+id/LinearLayout1"
  4. android:layout_width="fill_parent"
  5. android:layout_height="fill_parent"
  6. android:orientation="vertical"
  7. tools:context=".MainActivity" >
  8. <TextView
  9. android:layout_width="wrap_content"
  10. android:layout_height="wrap_content"
  11. android:text="请输入要保存的电话号码"
  12. />
  13. <EditText
  14. android:layout_width="fill_parent"
  15. android:layout_height="wrap_content"
  16. />
  17. <LinearLayout
  18. android:layout_width="fill_parent"
  19. android:layout_height="wrap_content"
  20. android:orientation="horizontal"
  21. android:gravity="right"
  22. >
  23. <Button
  24. android:layout_width="wrap_content"
  25. android:layout_height="wrap_content"
  26. android:text="保存"
  27. />
  28. <Button
  29. android:layout_width="wrap_content"
  30. android:layout_height="wrap_content"
  31. android:text="清空"
  32. />
  33. </LinearLayout>
  34. </LinearLayout>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/LinearLayout1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
tools:context=".MainActivity" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="请输入要保存的电话号码"
/>
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="right"
>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="保存"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="清空"
/>
</LinearLayout>
</LinearLayout> 


5.注意事项:

使用Layout_gravity的一个很重要的问题!!!

这个问题是一个读者偶尔一次发现反馈给我的,万分感谢,同时希望大家在看小猪博客的时候可以提出

一些实际开发中遇到的问题,以及解决方式,好给后来者经验!毕竟小猪不是神,不是什么方方面面都能

考虑到的,谢谢!


问题内容:

在一个LinearLayout的水平方向中布置两个TextView,想让一个左,一个右,怎么搞?

或许你会脱口而出:"gravity设置一个left,一个right就可以啦!"

真的这么简单?你试过吗?写个简单的Layout你就会发现,事与愿违了:

  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2. xmlns:tools="http://schemas.android.com/tools"
  3. android:layout_width="match_parent"
  4. android:layout_height="match_parent"
  5. android:orientation="horizontal"
  6. tools:context="com.jay.example.getscreendemo.MainActivity" >
  7. <TextView
  8. android:layout_width="wrap_content"
  9. android:layout_height="200dp"
  10. android:layout_gravity="left"
  11. android:background="#FF7878"
  12. android:gravity="center"
  13. android:text="O(∩_∩)O哈哈~" />
  14. <TextView
  15. android:layout_width="wrap_content"
  16. android:layout_height="200dp"
  17. android:layout_gravity="right"
  18. android:background="#FF7428"
  19. android:gravity="center"
  20. android:text="(*^__^*) 嘻嘻……" />
  21. </LinearLayout>

运行结果图:


看到这里你会说:哎呀,真的不行耶,要不在外层LinearLayout加个gravity=left的属性,然后设置第二个

TextView的layout_gravity为right,恩,好我们试一下:

  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2. xmlns:tools="http://schemas.android.com/tools"
  3. android:layout_width="match_parent"
  4. android:layout_height="match_parent"
  5. android:orientation="horizontal"
  6. android:gravity="left"
  7. tools:context="com.jay.example.getscreendemo.MainActivity" >
  8. <TextView
  9. android:layout_width="wrap_content"
  10. android:layout_height="200dp"
  11. android:background="#FF7878"
  12. android:gravity="center"
  13. android:text="O(∩_∩)O哈哈~" />
  14. <TextView
  15. android:layout_width="wrap_content"
  16. android:layout_height="200dp"
  17. android:layout_gravity="right"
  18. android:background="#FF7428"
  19. android:gravity="center"
  20. android:text="(*^__^*) 嘻嘻……" />
  21. </LinearLayout>

结果还是一样:

好吧,没辙了,怎么办好?

当 android:orientation="vertical" 时, 只有水平方向的设置才起作用,垂直方向的设置不起作用。

即:left,right,center_horizontal 是生效的。

当 android:orientation="horizontal" 时, 只有垂直方向的设置才起作用,水平方向的设置不起作用。

即:top,bottom,center_vertical 是生效的

不过貌似这个解决方法有点坑爹,比如如果只能竖直方向设置左右对齐的话,就会出现下面的效果:

显然不是我们要的结果把!

综上,要么按照上述给出的规则来布局,不过对于这种情况还是使用相对布局RelativeLayout把!

网上没给出具体的原因,都是说这样改有人说这个和orientation的优先级有关

,暂且先mark下来吧,后续如果知道原因的话再解释!前面屏幕适配也说过了,布局还是建议使用

RelativeLayout!

new UI-布局之LinearLayout(线性布局)详解相关推荐

  1. 常见界面布局之LinearLayout线性布局

    1.什么是LinearLayout线性布局 LinearLayout (线性布局)通常指定布局内的子控件水平或者轻直排列.在XML布局文件中定义线性布局的基本语法格式如下: <LinearLay ...

  2. Android精通:View与ViewGroup,LinearLayout线性布局,RelativeLayout相对布局,ListView列表组件...

    UI的描述 对于Android应用程序中,所有用户界面元素都是由View和ViewGroup对象构建的.View是绘制在屏幕上能与用户进行交互的一个对象.而对于ViewGroup来说,则是一个用于存放 ...

  3. 精通android布局,Android精通:View与ViewGroup,LinearLayout线性布局,RelativeLayout相对布局,ListView列表组件...

    标题图 UI的描述 对于Android应用程序中,所有用户界面元素都是由View和ViewGroup对象构建的.View是绘制在屏幕上能与用户进行交互的一个对象.而对于ViewGroup来说,则是一个 ...

  4. Android 应用开发(36)---LinearLayout(线性布局)

    LinearLayout(线性布局) 本节开始讲Android中的布局,Android中有六大布局,分别是: LinearLayout(线性布局),RelativeLayout(相对布局),Table ...

  5. 【Android 】零基础到飞升 | LinearLayout(线性布局)

    2.2.1 LinearLayout(线性布局) 本节引言 本节开始讲Android中的布局,Android中有六大布局,分别是: LinearLayout(线性布局),RelativeLayout( ...

  6. android线性布局设置控件固定在底部,Android UI组件LinearLayout线性布局详解

    LinearLayout 线性布局,该布局的继承关系: 1. 什么是线性布局 通俗的说感觉起来和线有关,参照线的特点,有么是横向的,要么是竖向的. LinearLayout是线性布局控件,它包含的子控 ...

  7. Android零基础入门第25节:最简单最常用的LinearLayout线性布局

    原文:Android零基础入门第25节:最简单最常用的LinearLayout线性布局 良好的布局设计对于UI界面至关重要,在前面也简单介绍过,目前Android中的布局主要有6种,创建的布局文件默认 ...

  8. Android布局——Linearlayout线性布局

    Android最简单最常用的LinearLayout线性布局 一.认识LinearLayout 线性布局是Android中较为常用的布局方式,使用LinearLayout标签.线性布局主要有两种形式, ...

  9. android linearlayout最大高度,Android中最简单最常用的LinearLayout线性布局

    原标题:Android中最简单最常用的LinearLayout线性布局 良好的布局设计对于UI界面至关重要,在前面也简单介绍过,目前Android中的布局主要有6种,创建的布局文件默认为Relativ ...

最新文章

  1. (每日一题)P3768 简单的数学题(确信)(莫反 + 欧拉反演 + 杜教筛 )
  2. jq处理 php数组,jQuery数组处理方法汇总_jquery
  3. 一种新的计算机视觉技术?将手机的摄像头变成了一个搜索引擎
  4. java 进程通信框架,MediatR-进程内的消息通信框架
  5. 关于运行robot framework 报错解决方法,ModuleNotFoundError: No module named 'robot'
  6. Tensorboard—使用keras结合Tensorboard可视化
  7. 报错:Navicat for MySQL 连接MySQL 8.0 出现1251错误
  8. java项目启动tomcat没报错,然后页面报404无法找到,重新编译后,出现以下状态...
  9. 点击开启此虚拟机时,出现“该虚拟机似乎正在使用中”问题
  10. 计算机网络之物理层:6、传输介质
  11. python的empty函数_python中numpy.empty()函数的用法
  12. 区块链入门与去中心化应用实战 之一 第3章 区块链技术核心原理实现
  13. java getname threads_Java8并发教程:Threads和Executors
  14. java 10套完整项目开发案例 (详细实现步骤)
  15. ListView嵌套GridView使用解析
  16. c#字符串的格式化输出
  17. SpringBoot配置Mybatis-Plus应用
  18. 找到抓手,用对方法,中电金信关于金融机构数据治理建设路径分享
  19. 中国科学院大学计算机考研信息汇总
  20. DBeaver执行.sql脚本报错:ERROR 1064 (42000) at line 1

热门文章

  1. 增加BUG的测试版本字段
  2. FastDFS 系统架构和功能原理
  3. 移动通信电波传播及损耗(二)
  4. php数组合并多维,PHP合并多维数组
  5. Emacs学习使用之路(一)
  6. HTML语言教程(完整版)
  7. Kindeditor的简单使用以及图片上传
  8. iOS证书(.p12)和描述文件(.mobileprovision)的导出和使用方法
  9. IAR for ARM系列教程(三)_菜单(Ⅰ)
  10. 使用宏模拟offsetof