2014-01-17 65 views
0

我知道這個話題已經討論過很多次了,但是我發現它很混亂,所以在試圖看看SO和Google之後,我決定問。我有一個Activity從中被調用。佈局定義如下(我發佈了我認爲相關的部分,如果需要額外的東西,只是要求它)。在代碼下面你會看到一些截圖。ListView:如何正確定義佈局?

<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:background="@drawable/rounded_edges" 
    android:paddingBottom="@dimen/activity_vertical_margin" 
    android:paddingLeft="@dimen/activity_horizontal_margin" 
    android:paddingRight="@dimen/activity_horizontal_margin" 
    android:paddingTop="@dimen/activity_vertical_margin" 
    android:orientation="vertical" 
    tools:context=".Bloqueados" > 

    <!-- TextView to set the activity tittle (I'm using a customized theme) --> 
    <TextView 
    ... /> 

    <!-- LinearLayout which shows the orange square --> 
    <LinearLayout 
     android:id="@+id/block_info" 
     android:background="@drawable/rounded_edges_orange" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" > 

     <EditText 
      ... > 
     </EditText> 
    </LinearLayout> 

    <!-- LinearLayout to ask for the user to be blocked --> 
    <LinearLayout 
     ... > 

     <EditText 
      ... > 

      <requestFocus /> 
     </EditText> 

     <ImageButton 
      ... /> 
    </LinearLayout>  

    <!-- Here it goes, the LinearLayout that contains the ListView --> 

    <LinearLayout 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content"  
     android:baselineAligned="false" 
     android:orientation="vertical" > 

     <!-- This TextView is shown only if the ListView is empty --> 
     <TextView 
     android:id="@+id/block_empty" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_marginTop="5dp" 
     android:paddingLeft="30dp" 
     android:text="@string/block_empty" 
     android:textStyle="italic" 
     android:textColor="#3085ef" /> 

     <!-- Else, this ListView is shown --> 
     <ListView 
     android:id="@+id/block_list" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:scrollbars="vertical" > 
     </ListView> 

     <!-- Save button --> 
     <Button 
     android:id="@+id/btn_block_save" 
     style="?android:attr/buttonStyleSmall" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_gravity="center" 
     android:layout_marginTop="10dp" 
     android:onClick="btn_close" 
     android:background="@drawable/botones" 
     android:textColor="#FFFFFF" 
     android:text="@string/btn_save" />  
    </LinearLayout> 

</LinearLayout> 

所以,從圖形上看,這是以前的截圖:

Former screenshow

任何異常。因此,讓我們填充ListView一些項目:

Full list

好了,還是沒什麼奇怪,但屏幕高度現在充滿。讓我們再向ListView添加一個項目。

Overflowed

這裏去的問題。 '保存'按鈕已經消失(好吧,它低於最後的ListView項目,但它超出了屏幕布局)。我知道使用ScrollView + ListView是一個不好的做法,所以即使我有一個完美的解決方法,我想避免使用它並正確地執行操作。所以問題是:如何讓ListView開始滾動,當下一個添加的項目將它下面的元素移出屏幕布局?

謝謝!

回答

2

layout_height =「wrap_content」表示增長,直到顯示所有的孩子。嘗試使用layout_height =「0dp」和layout_weight =「1」來代替。像這樣:

<ListView 
    android:id="@+id/block_list" 
    android:layout_width="fill_parent" 
    android:layout_height="0dp" 
    android:layout_weight="1" 
    android:scrollbars="vertical" > 
    </ListView> 
+0

如何簡單的解決方案有一些頭痛! :-) 非常感謝!還有一個問題:我意識到,在我測試的一些設備中,當放置在橫向時,高度底部佈局僅與列表中的第一個列表元素的少量匹配,使得幾乎不可能滾動。這當然是因爲'ListView'上面還有其他佈局元素。有沒有解決這個問題的方法? – nKn

+0

很高興幫助。請接受我的答案,如果它的工作!一種選擇是爲橫向模式創建另一個佈局,使列表視圖處於不同的位置。或者你可以讓它始終保持肖像。 –