LinearLayout(线性布局)

简介: 本节开始讲Android中的布局,今天我们要讲解的就是第一个布局,LinearLayout(线性布局),我们屏幕适配的使用用的比较多的就是LinearLayout的weight(权重属性),在这一节里,我们会详细地解析LinearLayout,包括一些基本的属性,Weight属性的使用,以及比例如何计算,另外还会说下一个用的比较少的属性:android:divider绘制下划线!

本节引言

本节开始讲Android中的布局,今天我们要讲解的就是第一个布局,LinearLayout(线性布局),我们屏幕适配的使用用的比较多的就是LinearLayout的weight(权重属性),在这一节里,我们会详细地解析LinearLayout,包括一些基本的属性,Weight属性的使用,以及比例如何计算,另外还会说下一个用的比较少的属性:android:divider绘制下划线!

1.本节学习图

网络异常,图片无法展示
|

2.weight(权重)属性详解

①最简单用法

如图:

网络异常,图片无法展示
|

实现代码:

<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,然后设置为androidweight属性设置比例即可;类推,竖直方向,只需设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>

运行效果图:

网络异常,图片无法展示
|

这个时候就会有疑问了,怎么会这样,这比例是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属性

setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,     
        LayoutParams.WRAP_CONTENT, 1));

3.为LinearLayout设置分割线

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

网络异常,图片无法展示
|

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

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

<View  
    android:layout_width="match_parent"  
    android:layout_height="1px"  
    android:background="#000000" />

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

②第二种则是使用LinearLayout的一个divider属性

直接为LinearLayout设置分割线这里就需要你自己准备一张线的图片了1)android:divider设置作为分割线的图片2)android:showDividers设置分割线的位置,none(无),beginning(开始),end(结束),middle(每两个组件间)3)dividerPadding设置分割线的Padding

使用示例:

网络异常,图片无法展示
|

实现代码:

<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>

4.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就可以啦!"真的这么简单?你试过吗?代码如下:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
    xmlns:tools="http://schemas.android.com/tools"  
    android:layout_width="match_parent"  
    android:layout_height="match_parent"  
    android:orientation="horizontal"  
    tools:context="com.jay.example.getscreendemo.MainActivity" >  
    <TextView  
        android:layout_width="wrap_content"  
        android:layout_height="200dp"  
        android:layout_gravity="left"  
        android:background="#FF7878"  
        android:gravity="center"  
        android:text="O(∩_∩)O哈哈~" />  
    <TextView  
        android:layout_width="wrap_content"  
        android:layout_height="200dp"  
        android:layout_gravity="right"  
        android:background="#FF7428"  
        android:gravity="center"  
        android:text="(*^__^*) 嘻嘻……" />  
</LinearLayout>

运行结果图:

网络异常,图片无法展示
|

看到这里你会说:哎呀,真的不行耶,要不在外层LinearLayout加个gravity=left的属性,然后设置第二个TextView的layout_gravity为right,好我们试一下:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
    xmlns:tools="http://schemas.android.com/tools"  
    android:layout_width="match_parent"  
    android:layout_height="match_parent"  
    android:orientation="horizontal"  
    android:gravity="left"  
    tools:context="com.jay.example.getscreendemo.MainActivity" >  
    <TextView  
        android:layout_width="wrap_content"  
        android:layout_height="200dp"  
        android:background="#FF7878"  
        android:gravity="center"  
        android:text="O(∩_∩)O哈哈~" />  
    <TextView  
        android:layout_width="wrap_content"  
        android:layout_height="200dp"  
        android:layout_gravity="right"  
        android:background="#FF7428"  
        android:gravity="center"  
        android:text="(*^__^*) 嘻嘻……" />  
</LinearLayout>

结果还是一样:

网络异常,图片无法展示
|

当 android:orientation="vertical" 时, 只有水平方向的设置才起作用,垂直方向的设置不起作用。即:left,right,center_horizontal 是生效的。当 android:orientation="horizontal" 时, 只有垂直方向的设置才起作用,水平方向的设置不起作用。即:top,bottom,center_vertical 是生效的。

然而,这方法好像并没有什么太大作用。比如:如果只能竖直方向设置左右对齐的话,就会出现下面的效果:

网络异常,图片无法展示
|

这显然不是我们要的结果。综上,要么按照上述给出的规则来布局,不过对于这种情况还是使用相对布局RelativeLayout把!网上没给出具体的原因,都是说这样改有人说这个和orientation的优先级有关,暂且先mark下来吧,后续如果知道原因的话再解释!前面屏幕适配也说过了,布局还是建议使用RelativeLayout!

相关文章
|
存储 大数据 API
大数据隐私保护策略:加密、脱敏与访问控制实践
【4月更文挑战第9天】本文探讨了大数据隐私保护的三大策略:数据加密、数据脱敏和访问控制。数据加密通过加密技术保护静态和传输中的数据,密钥管理确保密钥安全;数据脱敏通过替换、遮蔽和泛化方法降低敏感信息的敏感度;访问控制则通过用户身份验证和权限设置限制数据访问。示例代码展示了数据库、文件系统和API访问控制的实施方式,强调了在实际应用中需结合业务场景和平台特性定制部署。
3422 0
|
10月前
|
编解码 前端开发 UED
如何巧妙利用CSS3,打造炫酷视觉效果
在使用CSS3打造炫酷视觉效果时,要注意适度使用,避免过度设计导致页面过于复杂和混乱,影响用户的阅读和浏览体验。同时,要确保所使用的效果在不同的浏览器和设备上具有良好的兼容性,以提供一致的视觉效果。
195 1
|
SQL 存储 Oracle
常用数据库优缺点比较
【7月更文挑战第23天】常用数据库优缺点比较
1128 11
|
机器学习/深度学习 人工智能 自然语言处理
AIGC技术革新:智能创造如何重塑艺术与设计行业
AIGC技术,人工智能生成内容,正引领艺术与设计行业的变革。借助深度学习和自然语言处理等技术,AIGC能自动生成文本、图像等内容,丰富创作手段并提供创新机会。在艺术领域,它模拟各种风格作品,助力高效创作;在设计领域,它根据用户需求生成设计方案,提升个性化选择。AIGC打破了传统界限,提高了创作效率,并满足了用户的个性化需求。未来,随着技术进步和应用场景拓展,AIGC将在虚拟现实等领域的结合中,为艺术与设计带来更沉浸式、交互式的体验,重塑行业未来。【6月更文挑战第4天】
1239 1
|
机器学习/深度学习 搜索推荐 算法
AIGC让只为“一个人”打造广告成为可能
【1月更文挑战第1天】AIGC让只为“一个人”打造广告成为可能
229 1
AIGC让只为“一个人”打造广告成为可能
|
机器学习/深度学习 算法 自动驾驶
机器学习在智能交通系统中有许多应用
机器学习在智能交通系统中有许多应用
|
机器学习/深度学习 数据可视化 数据挖掘
R语言软件对房屋价格预测:回归、LASSO、决策树、随机森林、GBM、神经网络和SVM可视化|数据分享
R语言软件对房屋价格预测:回归、LASSO、决策树、随机森林、GBM、神经网络和SVM可视化|数据分享
|
监控
社交媒体营销
社交媒体营销
831 3
|
Java
Java 反射修改类的常量值、静态变量值、属性值
Java 反射修改类的常量值、静态变量值、属性值
1093 0
|
消息中间件 存储 中间件
【SpringCloud Stream消息驱动、设计思想以及整合rabbitmq消息队列案例--学习笔记】
【SpringCloud Stream消息驱动、设计思想以及整合rabbitmq消息队列案例--学习笔记】
479 0

热门文章

最新文章