天天看點

《我的Android進階之旅------>關于android:layout_weight屬性的詳細解析》《我的Android進階之旅------>關于android:layout_weight屬性的詳細解析》《我的Android進階之旅------>Android利用溫度傳感器實作帶動畫效果的電子溫度計》

最近碰到一個面試題,按照下圖,由Button和EditText組成的界面下廚布局代碼,解決這題目需要使用android:layout_weight的知識。

首先分析上圖所示的界面可以看成一下3個部分。

  1. 界面頂端的3個按鈕。
  2. 界面中間的EditText。
  3. 界面底端的1個按鈕。
  • 其中第1部分和第3部分分别在界面頂端和底端顯示,而第2部分的EditText充滿了剩餘的空間。
  • 如果想讓一個元件充滿整個螢幕,需要将android:layout_width和android:layout_height都設成fill_parent或者match_parent。我們可以将第1部分看成一個整體,android:layout_height的值設成wrap_content,第3部分的android:layout_height的值也設成wrap_content,那麼第2部分的<EditText>标簽的android:layout_weight屬性值要設為一個大于0的浮點數(例如1)。這樣EditText就會充滿整個剩餘空間。否則EditText會占用第3部分的Button空間,這樣界面底端的按鈕就會顯示不出來。
  • 現在來看第1部分的3個按鈕。這3個按鈕分鐘占了水準寬度的1/5、3/5、1/5。這種按比例擺放的元件一般都需要設定标簽的android:layout_weight屬性。按重要程度可将3個Button的android:layout_weight屬性從左到右依次設為2、1、2。

具體的實作布局代碼如下所示:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <!-- 頂端的3個按鈕布局 -->
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <!-- 該按鈕占1/5寬度 -->
        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="2"
            android:text="1/5" />
        
        <!-- 該按鈕占3/5寬度 -->
        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="3/5" />
        
        <!-- 該按鈕占1/5寬度 -->
        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="2"
            android:text="1/5" />
    </LinearLayout>
    
    <!-- 充滿剩餘空間的EditText元件 -->
    <EditText
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:text="EditText(充滿上方和下方按鈕之間的整個螢幕\n部落格位址:http://blog.csdn.net/ouyang_peng)" />
	
    <!-- 螢幕底端的按鈕 -->
    <Button android:layout_width="match_parent"
        android:layout_height="wrap_content" android:text="按鈕"/>
</LinearLayout>           

   可以通過另外一篇執行個體文章來了解android:layout_weight的屬性,連結如下:

《我的Android進階之旅------>關于android:layout_weight屬性的詳細解析》

(位址:http://blog.csdn.net/ouyang_peng/article/details/50757149)

《我的Android進階之旅------>Android利用溫度傳感器實作帶動畫效果的電子溫度計》

(位址為:http://blog.csdn.net/ouyang_peng/article/details/48790289)

    關于android:layout_weight的更多介紹可以參考一下資源:

  1. http://www.cnblogs.com/angeldevil/archive/2012/04/08/2437747.html
  2. http://blog.csdn.net/softmanfly/article/details/7636510
  3. http://developer.android.com/guide/topics/ui/layout/linear.html#Weight
  4. http://www.cnblogs.com/draem0507/archive/2013/05/11/3073508.html
  5. Android 對Layout_weight屬性完全解析以及使用ListView來實作表格

==================================================================================================

  作者:歐陽鵬  歡迎轉載,與人分享是進步的源泉!

  轉載請保留原文位址:

http://blog.csdn.net/ouyang_peng/article/details/9633781 http://blog.csdn.net/ouyang_peng/article/details/9634215 http://blog.csdn.net/ouyang_peng/article/details/9673081 http://blog.csdn.net/ouyang_peng

繼續閱讀