本节引言

本节开始讲Android中的布局,Android中有六大布局,分别是: LinearLayout(线性布局),RelativeLayout(相对布局),TableLayout(表格布局) FrameLayout(帧布局),AbsoluteLayout(�对布局),GridLayout(网格布局) 而今天我们�讲解的就是第一个布局,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,然�设置为android weight属性设置比例��;类推,竖直方�,�需设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就�以啦!" 真的这么简�?你试过�?写个简�的Layout你就会�现,事与愿�了: 代�如下:

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