Android Layoutその3:Linear Layout

4995 ワード


Android Layoutその3:Linear Layout
 
転載先:http://android.blog.51cto.com/268543/298345
 
ラインレイアウト
     
orientation-コンテナ内の要素の配置方法.vertical:サブエレメントは垂直に並び、horizontal:サブエレメントは水平に並ぶ.コードではsetOrientation()で動的に変更でき、値はそれぞれHORIZONTALまたはVERTICALです.
*Linear Layoutでは、幅/高さはコンポーネントの順番で1つずつ占められています!コンポーネントに「fill_parent」が設定されている場合、Layout_が設定されていません.Weightの場合、コンポーネントは残りのスペースを占有し、その後ろのコンポーネントは表示されません.下の図のEditTextのようにandroidを設定していない場合:layout_Weight=「1」で、その下の他のコンポーネントが見えなくなりました!
 
    
baselineAligned一般的に、このプロパティはデフォルトでtrueであり、同じ方向のコンポーネントが最初のコンポーネントに基づいて整列していることを示します.下図のtext 1,button 1,text 2は同じ水平線にあることがわかります.この効果が不要な場合は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!