布局之LinearLayout(线性布局)详解

本节引言:


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

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

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

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

用的比较多的就是LinearLayout的weight(权重属性),在这一节里,我们会说下一个用的比较少的属性:android:divider绘制下划线



1.为LinearLayout设置分割线

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

音乐的注册页面:


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

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

[html] view plaincopy在CODE上查看代码片派生到我的代码片

  1. <View  

  2.     android:layout_width="match_parent"  

  3.     android:layout_height="1px"  

  4.     android:background="#000000" />  

[html] view plain copy 在CODE上查看代码片派生到我的代码片

  1. <View  

  2.     android:layout_width="match_parent"  

  3.     android:layout_height="1px"  

  4.     android:background="#000000" />  

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



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

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

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

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

3)dividerPadding设置分割线的Padding

使用示例:


实现代码:

[html] view plaincopy在CODE上查看代码片派生到我的代码片

  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.   

  12.     <Button  

  13.         android:layout_width="wrap_content"  

  14.         android:layout_height="wrap_content"  

  15.         android:text="按钮1" />  

  16.   

  17.     <Button  

  18.         android:layout_width="wrap_content"  

  19.         android:layout_height="wrap_content"  

  20.         android:text="按钮2" />  

  21.   

  22.     <Button  

  23.         android:layout_width="wrap_content"  

  24.         android:layout_height="wrap_content"  

  25.         android:text="按钮3" />  

  26.   

  27. </LinearLayout>  

[html] view plain copy 在CODE上查看代码片派生到我的代码片

  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.   

  12.     <Button  

  13.         android:layout_width="wrap_content"  

  14.         android:layout_height="wrap_content"  

  15.         android:text="按钮1" />  

  16.   

  17.     <Button  

  18.         android:layout_width="wrap_content"  

  19.         android:layout_height="wrap_content"  

  20.         android:text="按钮2" />  

  21.   

  22.     <Button  

  23.         android:layout_width="wrap_content"  

  24.         android:layout_height="wrap_content"  

  25.         android:text="按钮3" />  

  26.   

  27. </LinearLayout>  


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

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



4.LinearLayout的简单例子:


实现代码如下:

[html] view plaincopy在CODE上查看代码片派生到我的代码片

  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.         

  9.     <TextView    

  10.         android:layout_width="wrap_content"    

  11.         android:layout_height="wrap_content"    

  12.         android:text="请输入要保存的电话号码"      

  13.         />    

  14.     <EditText    

  15.         android:layout_width="fill_parent"    

  16.         android:layout_height="wrap_content"     

  17.         />    

  18.     <LinearLayout    

  19.         android:layout_width="fill_parent"    

  20.         android:layout_height="wrap_content"    

  21.         android:orientation="horizontal"    

  22.         android:gravity="right"      

  23.         >    

  24.         <Button    

  25.             android:layout_width="wrap_content"    

  26.             android:layout_height="wrap_content"    

  27.             android:text="保存"    

  28.             />    

  29.         <Button    

  30.             android:layout_width="wrap_content"    

  31.             android:layout_height="wrap_content"    

  32.             android:text="清空"    

  33.         />    

  34.     </LinearLayout>    

  35. </LinearLayout>   

[html] view plain copy 在CODE上查看代码片派生到我的代码片

  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.         

  9.     <TextView    

  10.         android:layout_width="wrap_content"    

  11.         android:layout_height="wrap_content"    

  12.         android:text="请输入要保存的电话号码"      

  13.         />    

  14.     <EditText    

  15.         android:layout_width="fill_parent"    

  16.         android:layout_height="wrap_content"     

  17.         />    

  18.     <LinearLayout    

  19.         android:layout_width="fill_parent"    

  20.         android:layout_height="wrap_content"    

  21.         android:orientation="horizontal"    

  22.         android:gravity="right"      

  23.         >    

  24.         <Button    

  25.             android:layout_width="wrap_content"    

  26.             android:layout_height="wrap_content"    

  27.             android:text="保存"    

  28.             />    

  29.         <Button    

  30.             android:layout_width="wrap_content"    

  31.             android:layout_height="wrap_content"    

  32.             android:text="清空"    

  33.         />    

  34.     </LinearLayout>    

  35. </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.   

  8.     <TextView  

  9.         android:layout_width="wrap_content"  

  10.         android:layout_height="200dp"  

  11.         android:layout_gravity="left"  

  12.         android:background="#FF7878"  

  13.         android:gravity="center"  

  14.         android:text="O(∩_∩)O哈哈~" />  

  15.   

  16.     <TextView  

  17.         android:layout_width="wrap_content"  

  18.         android:layout_height="200dp"  

  19.         android:layout_gravity="right"  

  20.         android:background="#FF7428"  

  21.         android:gravity="center"  

  22.         android:text="(*^__^*) 嘻嘻……" />  

  23.   

  24. </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.   

  9.     <TextView  

  10.         android:layout_width="wrap_content"  

  11.         android:layout_height="200dp"  

  12.         android:background="#FF7878"  

  13.         android:gravity="center"  

  14.         android:text="O(∩_∩)O哈哈~" />  

  15.   

  16.     <TextView  

  17.         android:layout_width="wrap_content"  

  18.         android:layout_height="200dp"  

  19.         android:layout_gravity="right"  

  20.         android:background="#FF7428"  

  21.         android:gravity="center"  

  22.         android:text="(*^__^*) 嘻嘻……" />  

  23.   

  24. </LinearLayout>  

结果还是一样:



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

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

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

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

即:top,bottom,center_vertical 是生效的


 

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


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

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

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

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

RelativeLayout!


转载于:https://my.oschina.net/leohouse/blog/662902

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值