2013-04-24 36 views
7

我有一個對話框,其中包含ScrollView內部的一個列表(中的一堆TextView)。佈局如下:Scrollview使底部視圖在對話框中消失

<?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" > 

    <ProgressBar 
     android:id="@+id/delete_progress" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:visibility="gone" /> 

    <ScrollView 
     android:id="@+id/filename_scroll" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" > 

     <LinearLayout 
      android:id="@+id/filename_container" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:orientation="vertical" > 
     </LinearLayout> 
    </ScrollView> 

    <View 
     android:id="@+id/horisontal_separator" 
     android:layout_width="fill_parent" 
     android:layout_height="1dp" 
     android:background="@android:color/darker_gray" /> 

    <LinearLayout 
     android:id="@+id/button_container" 
     android:layout_width="match_parent" 
     android:layout_height="48dp" > 

     <Button 
      android:id="@+id/load_button" 
      android:layout_width="0dp" 
      android:layout_height="wrap_content" 
      android:text="@string/button_load" 
      android:gravity="center" 
      android:layout_weight="1" /> 

     <View 
      android:layout_width="1dp" 
      android:layout_height="fill_parent" 
      android:background="@android:color/darker_gray" /> 

     <Button 
      android:id="@+id/delete_button" 
      android:layout_width="0dp" 
      android:layout_height="wrap_content" 
      android:text="@string/button_delete" 
      android:gravity="center" 
      android:layout_weight="1" /> 
    </LinearLayout> 

</LinearLayout> 

它看起來像這樣,只有在名單上的幾個項目: A few items on the list

但是,當有不止可以適合在屏幕上(並有實際需要滾動) ,我的按鈕被推到屏幕下方。當所有的方式滾動至底部,它看起來像這樣: A lot of items on the list

我需要包含按鈕繼續擔任頁腳LinearLayout,它不應該在任何地方滾動,顯然不會消失。我試過擺弄佈局高度和重量,但無濟於事。

+0

您是否嘗試將ListView設置爲固定高度,例如200dip? – Demonick 2013-04-24 08:55:03

+0

我沒有實際的ListView,但改變LinearLayout的高度並沒有什麼區別。如果我改變ScrollView的高度,它會再次按下按鈕。 – j0ntech 2013-04-24 08:58:03

回答

19

嘗試滾動型

<ScrollView 
    android:id="@+id/filename_scroll" 
    android:layout_width="match_parent" 
    android:layout_weight="1" 
    android:layout_height="0dp" > 
+0

這工作得很好。你能解釋爲什麼以及如何發生? – 2017-01-10 12:30:56

+0

這也適用於我!會喜歡解釋爲什麼這個工程。 – Vinnie 2017-02-01 19:18:40

0

改變,我認爲你應該提到了滾動的layout_height明確像

<ScrollView 
    android:id="@+id/filename_scroll" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" > ////say android:layout_height="50dp" 

    <LinearLayout 
     android:id="@+id/filename_container" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:orientation="vertical" > 
    </LinearLayout> 
</ScrollView> 

這樣。另一個選擇是,通過顯式指定layout_height引入外部組件,如表格佈局,並按原樣包含您的滾動視圖。

希望工程下面的XML佈局要顯示自定義對話框

+0

我不想明確指定任何內容,因爲它應該根據列表中項目的數量更改大小。無論如何,umesh的回答完全符合我的需求。 – j0ntech 2013-04-24 09:13:35

+0

它的作品適合我。滾動視圖用作容器,我使用動態內容。超過20諾斯。無論如何,很高興你知道這件事 – 2013-04-24 10:25:24

0

使用,

<FrameLayout 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" > 

    <ListView 
     android:id="@+id/gameList" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_marginBottom="30dip" /> 

    <LinearLayout 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_gravity="bottom" > 

     <Button 
      android:id="@+id/load" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:layout_weight="1" 
      android:text="Load" /> 

     <Button 
      android:id="@+id/delete" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:layout_weight="1" 
      android:text="Delete" /> 
    </LinearLayout> 
</FrameLayout> 

而且從這樣調用你的類文件,

>  Button getList = (Button) findViewById(R.id.getList); 
>  getList.setOnClickListener(new View.OnClickListener() { 
>  
>    @Override 
>  public void onClick(View v) { 
>  // TODO Auto-generated method stub 
>  final Dialog listDialog = new Dialog(MainActivity.this); 
>  listDialog.setTitle("Load a game"); 
>  listDialog.setContentView(R.layout.custom_dialog); 
>  
>  ListView gameList = (ListView) listDialog.findViewById(R.id.gameList); 
>  gameList.setAdapter(new ArrayAdapter<String>   (MainActivity.this,android.R.layout.simple_list_item_1,new String[] {"your array" })); 
>  
>  Button dismiss = (Button) listDialog.findViewById(R.id.load); 
>  dismiss.setOnClickListener(new View.OnClickListener() { 
>  
>   @Override 
>   public void onClick(View v) { 
>    // TODO Auto-generated method stub 
>    listDialog.dismiss(); 
>      } 
>   }); 
>  
>   listDialog.show(); 
>  
>   } }); 
+0

'marginBottom'的東西很聰明,但我已經得到了答案。另外,我的原始佈局甚至沒有'ListView'。你甚至讀過這個問題嗎? – j0ntech 2013-04-24 11:24:29

+0

你好jOntech,我已經閱讀過它,我建議你替代它的解決方案。這個樣本服務於你的目的,它運行sample.Did你檢查它? – 2013-04-24 11:57:39