天天看點

android布局LinearLayout的使用

序:本文講述Android布局中的LinearLayout的使用,重點2方面:

1. 跨行和跨列如何實作 

2.使用layout_weight注意事項

-------------------------------------------------------------------------------------------------------------------------------------------

1. 跨列如何實作?(例如一行 有2個按鈕,其中左邊一個按鈕長度是右邊按鈕的2倍,如下圖的按鈕0)。

android布局LinearLayout的使用

實作方法:a. 先設定 按鈕0  的layout_weight = "2" , layout_width=“0dp”

                  b. 再設定 按鈕·   的layout_weight = "1" , layout_width=“0dp” 即可。

2. 使用layout_weight注意事項

       在設定layout_weight的view(比如Button) 或Component(比如LinearLayout)設定layout_weight值時,要将對應的view(比如Button) 或Component(比如LinearLayout)的layout_width設定為“0dp” ,這樣系統能自動按比例配置設定空間。因為之前的文章也講了,layout_weight的權重,是将系統預設配置設定後剩餘的空間(因為系統剛開始會根據layout_width的值先自動配置設定空間)按比例配置設定。是以不寫layout_width="match_parent",而寫成 layout_width=“0dp”.這樣是說,系統剛開始不自動配置設定空間,是以剩餘的空間就是初始空間,就能按比例配置設定了。

簡單概括:一行橫向有2個按鈕:button1,button2.  使得button1的寬 : button2寬的 = 2:1 的方法如下

button1 : android:layout_weight = "2" , android:layout_width="0dp"

button2 : android:layout_weight = "1" , android:layout_width="0dp" 

同理:

對于component(比如LinearLayout)設定比例寬度方法一緻,以下圖舉例。

android布局LinearLayout的使用

如上圖,左邊LinearLayout寬 : 右邊LinearLayout寬 = 3 : 1 。實作方法如下:

将一個大的LinearLayout 分成 左邊一個LinearLayout 和 右邊一個LinearLayout ,寬度比為 3:1 。關鍵代碼如下:

強調1.左邊寬3倍設定方法:    android:layout_width="0dp" android:layout_weight="3"

強調2.右邊寬1倍設定方法:    android:layout_width="0dp" android:layout_weight="1"

<LinearLayout 
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"  >  
        
             <LinearLayout 
	        android:orientation="vertical"
	        android:layout_width="0dp"
	        android:layout_height="wrap_content"
	        android:layout_weight="3"  > 
	        
                            省略中間具體内容
            
             
	     </LinearLayout><!-- 對應兩行整體左邊 2/3 -->  
	     
	     <LinearLayout         
	        android:layout_width="0dp"
	        android:layout_height="match_parent"
	        android:layout_weight="1"  >
             
	                       省略中間具體内容	         
	         
	     </LinearLayout><!-- 對應兩行整體右邊 1/3 --> 
     </LinearLayout><!-- 總體linearlayout --> 
           

-------------------------------------------------------------------------------------------------------------------------------------------

實際程式結果圖:

android布局LinearLayout的使用

頁面代碼:/res/layout/activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
	android:layout_width="match_parent"
	android:layout_height="match_parent"
	android:orientation="vertical" >
	<EditText
		android:layout_width="match_parent"
		android:layout_height="wrap_content" >
	</EditText>
	<LinearLayout
		android:layout_width="match_parent"
		android:layout_height="wrap_content"
		android:orientation="horizontal" >
             "
		<Button
			android:layout_width="0dp"
			android:layout_height="wrap_content"
			android:layout_weight="1"
			android:text="+" />
		<Button
			android:layout_width="0dp"
			android:layout_height="wrap_content"
			android:layout_weight="1"
			android:text="-" />
		<Button
			android:layout_width="0dp"
			android:layout_height="wrap_content"
			android:layout_weight="1"
			android:text="*" />
		<Button
			android:layout_width="0dp"
			android:layout_height="wrap_content"
			android:layout_weight="1"
			android:text="/" />
	</LinearLayout>
	<LinearLayout
		android:layout_width="match_parent"
		android:layout_height="wrap_content"
		android:orientation="horizontal" >
		<LinearLayout
			android:layout_width="0dp"
			android:layout_height="wrap_content"
			android:layout_weight="3"
			android:orientation="vertical" >
			<LinearLayout
				android:layout_width="match_parent"
				android:layout_height="match_parent"
				android:orientation="horizontal" >
				<Button
					android:layout_width="0dp"
					android:layout_height="wrap_content"
					android:layout_weight="1"
					android:text="1" />
				<Button
					android:layout_width="0dp"
					android:layout_height="wrap_content"
					android:layout_weight="1"
					android:text="2" />
				<Button
					android:layout_width="0dp"
					android:layout_height="wrap_content"
					android:layout_weight="1"
					android:text="3" />
			</LinearLayout> <!-- 對應左邊2/3 的上一行 -->
 
			<LinearLayout
				android:layout_width="match_parent"
				android:layout_height="match_parent"
				android:orientation="horizontal" >
				<Button
					android:layout_width="0dp"
					android:layout_height="wrap_content"
					android:layout_weight="2"
					android:text="0" />
				<Button
					android:layout_width="0dp"
					android:layout_height="wrap_content"
					android:layout_weight="1"
					android:text="." />
			</LinearLayout><!-- 對應左邊2/3 的下一行 -->

 		</LinearLayout> <!-- 對應下兩行整體左邊 2/3 -->
 
		<LinearLayout
			android:layout_width="0dp"
			android:layout_height="match_parent"
			android:layout_weight="1" >
			<Button
				android:layout_width="match_parent"
				android:layout_height="match_parent"
				android:text="=" >
			</Button>
		</LinearLayout> <!-- 對應下兩行整體右邊 1/3 -->
 
	</LinearLayout><!-- 對應下面兩大行的總體linearlayout -->
 
</LinearLayout>
           

 

繼續閱讀