Android Layout之三:Linear Layout

 

Android Layout之三:Linear Layout

 

转载自:http://android.blog.51cto.com/268543/298345

 

线形布局

      orientation - 容器内元素的排列方式。vertical: 子元素们垂直排列,horizontal: 子元素们水平排列。在代码里可通过setOrientation()进行动态改变,值分别为HORIZONTAL或者VERTICAL。
     * 在Linear Layout,  宽度/高度都是按着组件的次序逐个占用的!所以当某个组件设置"fill_parent",在没有设置Layout_weight的情况下,该组件会占用了余下的空间,那么在它后面的组件就会显示不出来。如下图的EditText如果没有设置android:layout_weight="1", 它下面的其他组件就看不见了!
 
     baselineAligned 一般情况下,这个属性默认为true,代表在同一方向的组件都基于第一个组件对齐。所以可以看到下图的text1, button1, text2是在同一水平线的。当不需要这效果时,可以设置为false。
main.xml配置内容:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<TextView         
	android:text="This is a example of linear layout."         
	android:layout_width="fill_parent"          
	android:layout_height="wrap_content"          
	/>     
<EditText         
	android:layout_width="fill_parent"         
	android:layout_height="fill_parent"         
	android:layout_weight="1"         
	android:id="@+id/edittext"         
	/>      
<LinearLayout         
	android:id="@+id/LinearLayout01"          
	android:layout_width="fill_parent"          
	android:layout_height="wrap_content"          
	android:orientation="horizontal"
	>         
	<TextView              
		android:text="text1"              
		android:id="@+id/TextView01"              
		android:layout_width="wrap_content"              
		android:layout_height="wrap_content"             
		/>         
	<Button 
		android:text="Button01"              
		android:id="@+id/Button01"              
		android:layout_width="fill_parent"              
		android:layout_height="wrap_content"             
		android:layout_weight="1"             
		/>         
	<TextView              
		android:text="text2"              
		android:id="@+id/TextView02"              
		android:layout_width="wrap_content"              
		android:layout_height="wrap_content"             
		/>     
</LinearLayout>      
<TextView         
	android:layout_width="wrap_content"         
	android:layout_height="wrap_content"         
	android:text="buttom"         
/> 
</LinearLayout>
 
结果如下图:


 

 

 

如果将main.xml配置内容中:

<EditText         
	android:layout_width="fill_parent"         
	android:layout_height="fill_parent"         
	android:layout_weight="1"         
	android:id="@+id/edittext"         
	/> 

 

更改为:

 

<EditText         
	android:layout_width="fill_parent"         
	android:layout_height="fill_parent"         
	android:layout_weight="0"         
	android:id="@+id/edittext"         
	/> 

 

或者:

<EditText         
	android:layout_width="fill_parent"         
	android:layout_height="fill_parent"                 
	android:id="@+id/edittext"         
	/> 

 

那么,结果如下图:

 



 

 

发现组件edittext将下面的组件全部挡住了,这其中就数属性 【layout_weight 】导致的。

 

layout_weight - 重要度

     个人理解为显示的优先级。默认为0(最高),数值越大,优先级越低!参考下面的Linear Layout例子。要让layout_weight生效,需要父层或父父层的相应layout_width/layout_height = "fill_parent!

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值