深入LinearLayout


1. LinearLayout布局的嵌套

2. 奇葩的 layout_weight 属性

布局是一种不可见的控件

代码如下

 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="wrap_content"   
 5     android:background="#FF0000"
 6     android:orientation="horizontal"   
 7     tools:context="first.pack.MainActivity$PlaceholderFragment" >
 8  
 9     <LinearLayout
10         android:layout_width="wrap_content"
11         android:layout_height="wrap_content"
12         android:layout_margin="20dp"   //间隔
13         android:orientation="vertical"   
14         android:background="#880000">
15         <TextView
16             android:layout_width="wrap_content"
17             android:layout_height="wrap_content"
18             android:textSize="20sp"    //文字大小
19             android:text="first"/>
20         <TextView
21             android:layout_width="wrap_content"
22             android:layout_height="wrap_content"
23             android:textSize="20sp"
24             android:text="second"/>
25     </LinearLayout>
26     
27     <LinearLayout
28         android:layout_width="wrap_content"
29         android:layout_height="wrap_content"
30         android:layout_margin="20dp"
31         android:orientation="vertical"   
32         android:background="#880000">
33         <TextView
34             android:layout_width="wrap_content"
35             android:layout_height="wrap_content"
36             android:textSize="20sp"
37             android:text="third"/>
38         <TextView
39             android:layout_width="wrap_content"
40             android:layout_height="wrap_content"
41             android:textSize="20sp"
42             android:text="fourth"/>
43     </LinearLayout>
44     
45 </LinearLayout>

效果如下

2. 奇葩的 layout_weight 属性

使用条件:

代码如下:

 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="wrap_content"   
 5     android:background="#FF0000"
 6     android:orientation="horizontal"   
 7     tools:context="first.pack.MainActivity$PlaceholderFragment" >
 8  
 9     <TextView
10             android:layout_width="wrap_content"
11             android:layout_height="wrap_content"
12             android:background="#00FF00"
13             android:textSize="20sp"
14             android:text="first"/>
15     <TextView
16             android:layout_width="wrap_content"
17             android:layout_height="wrap_content"
18             android:background="#0000FF"
19             android:textSize="20sp"
20             android:text="secondddddd"/>    
21             
22 </LinearLayout>

加入Layout_weight属性之后

 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="wrap_content"   
 5     android:background="#FF0000"
 6     android:orientation="horizontal"   
 7     tools:context="first.pack.MainActivity$PlaceholderFragment" >
 8  
 9     <TextView
10             android:layout_width="wrap_content"
11             android:layout_height="wrap_content"
12             android:background="#00FF00"
13             android:textSize="20sp"
14             android:layout_weight="1"   //加入weight属性
15             android:text="first"/>
16     <TextView
17             android:layout_width="wrap_content"
18             android:layout_height="wrap_content"
19             android:background="#0000FF"
20             android:textSize="20sp"
21             android:layout_weight="1"
22             android:text="secondddddd"/>    
23             
24 </LinearLayout>

两个红色的箭头长短平均分配的!!!!

但是这不能保证两个控件大小一样,因此将本身控件width设置为0dp就好了!!!

 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="wrap_content"   
 5     android:background="#FF0000"
 6     android:orientation="horizontal"   
 7     tools:context="first.pack.MainActivity$PlaceholderFragment" >
 8  
 9     <TextView
10         android:layout_width="0dp"
11         android:layout_height="wrap_content"
12         android:background="#00FF00"
13         android:layout_weight="1"
14         android:text="First"/>
15     <TextView
16         android:layout_width="0dp"
17         android:layout_height="wrap_content"
18         android:background="#0000FF"
19         android:layout_weight="1"
20         android:text="Second"/>
21     
22 </LinearLayout>

优质内容筛选与推荐>>
1、【题解】 bzoj1135: [POI2009]Lyz (线段树+霍尔定理)
2、201521123002《Java程序设计》第14周学习总结
3、图的遍历之 深度优先搜索和广度优先搜索
4、《大道至简》的读后感
5、opencv3.0 glcm算法


长按二维码向我转账

受苹果公司新规定影响,微信 iOS 版的赞赏功能被关闭,可通过二维码转账支持公众号。

    阅读
    好看
    已推荐到看一看
    你的朋友可以在“发现”-“看一看”看到你认为好看的文章。
    已取消,“好看”想法已同步删除
    已推荐到看一看 和朋友分享想法
    最多200字,当前共 发送

    已发送

    朋友将在看一看看到

    确定
    分享你的想法...
    取消

    分享想法到看一看

    确定
    最多200字,当前共

    发送中

    网络异常,请稍后重试

    微信扫一扫
    关注该公众号





    联系我们

    欢迎来到TinyMind。

    关于TinyMind的内容或商务合作、网站建议,举报不良信息等均可联系我们。

    TinyMind客服邮箱:support@tinymind.net.cn