Android Layout之三:Linear Layout

系统 1329 0

 

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>

      
 
结果如下图:

Android Layout之三:Linear Layout
 

 

 

如果将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"         
	/> 
  

 

那么,结果如下图:

 


Android Layout之三:Linear Layout
 

 

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

 

layout_weight - 重要度

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

 

 

Android Layout之三:Linear Layout


更多文章、技术交流、商务合作、联系博主

微信扫码或搜索:z360901061

微信扫一扫加我为好友

QQ号联系: 360901061

您的支持是博主写作最大的动力,如果您喜欢我的文章,感觉我的文章对您有帮助,请用微信扫描下面二维码支持博主2元、5元、10元、20元等您想捐的金额吧,狠狠点击下面给点支持吧,站长非常感激您!手机微信长按不能支付解决办法:请将微信支付二维码保存到相册,切换到微信,然后点击微信右上角扫一扫功能,选择支付二维码完成支付。

【本文对您有帮助就好】

您的支持是博主写作最大的动力,如果您喜欢我的文章,感觉我的文章对您有帮助,请用微信扫描上面二维码支持博主2元、5元、10元、自定义金额等您想捐的金额吧,站长会非常 感谢您的哦!!!

发表我的评论
最新评论 总共0条评论