2011-02-27 236 views
2

我不能爲我的生活弄清楚這一點。我試圖佈局如下(垂直):Android佈局問題

ImageView的(填充所有可用空間) 的TextView(以根據需要垂直空間) 的EditText(以根據需要垂直空間) 按鈕按鈕按鈕(等距)

我有以下佈局設置。圖像視圖目前設置爲200dip。這個值應該填充所有可用空間?其他組件應該是第一位的,圖像視圖應該得到剩下的部分。我在這方面搜索了一個例子,還沒有發現任何東西。

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
       android:layout_width="fill_parent" 
       android:layout_height="fill_parent" > 

    <ImageView android:id="@+id/imgView" 
       android:layout_width="fill_parent" 
       android:layout_height="200dip"    
       android:layout_alignParentTop="true" /> 

    <TextView android:id="@+id/lblDesc" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:text="@string/PhotoDesc"    
      android:layout_below="@id/imgView" /> 

    <EditText android:id="@+id/edtDesc" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content"       
      android:layout_below="@id/lblDesc" /> 

    <!-- Bottom layout contains the three buttons - Take Photos, Transmit, and Setup. --> 
    <LinearLayout android:layout_width="fill_parent" 
       android:layout_height="wrap_content" 
       android:layout_alignParentBottom="true" 
       android:orientation="horizontal" 
       android:weightSum="3"> 

     <!-- Take photos button --> 
     <Button android:id="@+id/btnCamera" 
       android:gravity="center_vertical|center_horizontal" 
       android:layout_width="fill_parent"    
       android:layout_height="wrap_content" 
       android:layout_weight="1"    
       android:text="@string/TakePhotos" /> 

     <!-- Transmit button --> 
     <Button android:id="@+id/btnUpload" 
       android:gravity="center_vertical|center_horizontal" 
       android:layout_width="fill_parent"    
       android:layout_height="wrap_content" 
       android:layout_weight="1"    
       android:text="@string/Upload" />    

     <!-- Setup button --> 
     <Button android:id="@+id/btnSetup" 
       android:gravity="center_vertical|center_horizontal"    
       android:layout_width="fill_parent" 
       android:layout_height="wrap_content" 
       android:text="@string/Setup"    
       android:layout_weight="1" /> 
    </LinearLayout>      

</RelativeLayout> 

回答

4

看起來像所有你需要的是一個LinearLayout與上ImageView適當的重量拿剩下的空間。

layout_weight與LinearLayout經常被誤解。 LinearLayout兩次測量其子女。首先,根據給定的layout_width或layout_height對孩子進行測量,然後在測量完所有孩子後,根據其權重劃分剩餘的剩餘空間。

這意味着兩件重要的事情:

  • 你不願意去說match_parent(或FILL_PARENT舊版本)與佈局的方向的權重。 (即,layout_width="match_parent"對於LinearLayout而對於orientation="horizontal"layout_height="match_parent"對於orientation="vertical")。match_parent將首先生效,消耗所有當前可用的空間並且剩餘的剩餘子女不留下。
  • 如果要在具有權重的多個視圖之間平均劃分空間,或者使一個視圖消耗任何空間而不考慮實際內容大小,請使用0dip作爲layout_widthlayout_height值。

試試這個佈局:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:orientation="vertical" > 

    <ImageView android:id="@+id/imgView" 
      android:layout_width="match_parent" 
      android:layout_height="0dip"    
      android:layout_weight="1" /> 

    <TextView android:id="@+id/lblDesc" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:text="@string/PhotoDesc"    
     android:layout_below="@id/imgView" /> 

    <EditText android:id="@+id/edtDesc" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content"       
     android:layout_below="@id/lblDesc" /> 

    <!-- Bottom layout contains the three buttons - Take Photos, Transmit, and Setup. --> 
    <LinearLayout android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:orientation="horizontal" 
      android:weightSum="3"> 

     <!-- Take photos button --> 
     <Button android:id="@+id/btnCamera" 
      android:gravity="center_vertical|center_horizontal" 
      android:layout_width="0dip"    
      android:layout_height="wrap_content" 
      android:layout_weight="1"    
      android:text="@string/TakePhotos" /> 

     <!-- Transmit button --> 
     <Button android:id="@+id/btnUpload" 
      android:gravity="center_vertical|center_horizontal" 
      android:layout_width="0dip"    
      android:layout_height="wrap_content" 
      android:layout_weight="1"    
      android:text="@string/Upload" />    

     <!-- Setup button --> 
     <Button android:id="@+id/btnSetup" 
      android:gravity="center_vertical|center_horizontal"    
      android:layout_width="0dip" 
      android:layout_height="wrap_content" 
      android:text="@string/Setup"    
      android:layout_weight="1" /> 
    </LinearLayout>      

</LinearLayout> 
+0

太棒了,兩個答案一次!兩者都是正確的,但是關於佈局間隔如何劃分的簡單解釋非常有用。我在Android開發的第二天,我只是沒有得到它!謝謝! – Paul

+1

我不想用兩個額外的細節來解決答案,但既然你喜歡額外的信息,這裏有一個有趣的提示。如果您對LinearLayout的子項使用0dip/weight技術,它將跳過該子項的初始測量階段,並僅依賴於重量測量階段。 (沒有體重的孩子也只能依靠一次傳球。)如果這個孩子實際上是一個需要一些時間來衡量的複雜子層次,這可能是一個重要的優化。 – adamp

3

更改RelativeLayoutLinearLayoutandroid:orientation="vertical",再加入android:layout_weight="1"到您的ImageView。

下面的示例代碼。我將ImageView的背景設置爲紅色以查看效果。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
       android:layout_width="fill_parent" 
       android:layout_height="fill_parent" 
       android:orientation="vertical"> 

    <ImageView android:id="@+id/imgView" 
       android:layout_width="fill_parent" 
       android:layout_height="0dp"    
       android:layout_alignParentTop="true" 
       android:layout_weight="1" 
       android:background="#FF0000" /> 

    <TextView android:id="@+id/lblDesc" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:text="test" /> 

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

    <!-- Bottom layout contains the three buttons - Take Photos, Transmit, and Setup. --> 
    <LinearLayout android:layout_width="fill_parent" 
       android:layout_height="wrap_content" 
       android:layout_alignParentBottom="true" 
       android:orientation="horizontal" 
       android:weightSum="3"> 

     <!-- Take photos button --> 
     <Button android:id="@+id/btnCamera" 
       android:gravity="center_vertical|center_horizontal" 
       android:layout_width="fill_parent"    
       android:layout_height="wrap_content" 
       android:layout_weight="1"    
       android:text="btn1" /> 

     <!-- Transmit button --> 
     <Button android:id="@+id/btnUpload" 
       android:gravity="center_vertical|center_horizontal" 
       android:layout_width="fill_parent"    
       android:layout_height="wrap_content" 
       android:layout_weight="1"    
       android:text="btn2" />    

     <!-- Setup button --> 
     <Button android:id="@+id/btnSetup" 
       android:gravity="center_vertical|center_horizontal"    
       android:layout_width="fill_parent" 
       android:layout_height="wrap_content" 
       android:text="btn3"    
       android:layout_weight="1" /> 
    </LinearLayout>      

</LinearLayout>