2014-02-10 24 views
1

我想用AlertDialog下面的按鈕顯示一個列表。按鈕是關閉對話框本身。我擴展AlertDialog並添加了一些LayoutParamsWindow延長整個屏幕寬度的對話框:AlertDialog中的ListView下的按鈕被隱藏

//Grab the window of the dialog, and change the width 
    WindowManager.LayoutParams lp = new WindowManager.LayoutParams(); 
    Window window = getWindow(); 
    lp.copyFrom(window.getAttributes()); 
    //This makes the dialog take up the full width 
    lp.width = WindowManager.LayoutParams.MATCH_PARENT; 
    lp.height = WindowManager.LayoutParams.WRAP_CONTENT; 
    window.setAttributes(lp); 

確定。但是,如果列表很長(顯示已滿),我可以滾動ListView,但該按鈕不顯示在其下。這裏是對話框的ContentView:

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

<ListView 
    android:id="@+id/choose_equipment_list" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" /> 

<Button 
    android:layout_below="@+id/choose_equipment_list" 
    android:id="@+id/btn_choose_equipment_close" 
    style="@style/custom_button" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="close" /> 

</RelativeLayout> 

我該怎麼做才能在列表中顯示按鈕ALWAYS,不管它多長時間? (我讀了很多警告,把一個ListView一個ScrollView內,嘗試也無妨和失敗...)

+0

在alertdialog上使用'setPositiveButton',就是這麼做的。 – njzk2

回答

5

試試:

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

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

    <Button 
     android:id="@+id/btn_choose_equipment_close" 
     style="@style/custom_button" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="close" /> 

</LinearLayout> 

如果不行,除去style="@style/custom_button"線。某些樣式配置可能會改變視圖樣式並隱藏按鈕。

+1

它做了這個工作(沒有刪除風格,我需要的方式;))對不起,沒有測試其他答案導致這一個工作,但謝謝大家的所有答案! – lis

0

我想用AlertDialog下面的按鈕顯示一個列表。

所以,你要做的是:

從您的按鈕刪除此:

android:layout_below="@+id/choose_equipment_list" 

然後把這個按鈕:

android:layout_alignParentBottom="true" 

,並把這個ListView中:

android:layout_above="@+id/btn_choose_equipment_close" 

您已完成。

0

嗯。也許嘗試使用碎片。我爲你編碼樣本。告訴我,如果這是你在找什麼:)

MainActivity.java

public class MainActivity extends ActionBarActivity { 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main_activity); 
     findViewById(R.id.open_dialog_button).setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View view) { 
       SampleDialogFragment dialogFragment = new SampleDialogFragment(); 
       dialogFragment.show(getSupportFragmentManager(), "dialogFragment"); 
      } 
     }); 
    } 
} 

main_activity.xml

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

    <Button 
     android:id="@+id/open_dialog_button" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Open dialog" 
     android:layout_gravity="center" /> 
</FrameLayout> 

SampleDialogFragment.java

public class SampleDialogFragment extends DialogFragment { 

    @Override 
    public Dialog onCreateDialog(Bundle savedInstanceState) { 
     AlertDialog.Builder alert = new AlertDialog.Builder(getActivity()); 
     alert.setTitle("Timer"); 
     alert.setPositiveButton("OK", null); 

     View view = View.inflate(getActivity(), R.layout.dialog, null); 

     String[] array = {"Hello", "Hello", "Hello", "Hello", "Hello", "Hello", "Hello", "Hello", "Hello", "Hello", "Hello", "Hello", "Hello", "Hello", "Hello"}; 

     ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, array); 
     ((ListView) view.findViewById(R.id.listView)).setAdapter(adapter); 

     alert.setView(view); 

     return alert.create(); 
    } 
} 

dialog.xml

<ListView xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/listView" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" /> 
0

我認爲它可能是能夠讓你在你的第一個按鈕,位於底部的XML聲明需要什麼,然後它上面的列表視圖與高度設置爲FILL_PARENT

0

這應該是你想要的東西..

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

<ListView 
    android:id="@+id/choose_equipment_list" 
    android:layout_above="@+id/btn_choose_equipment_close" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" /> 

<Button 
    android:id="@+id/btn_choose_equipment_close" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignParentBottom="true" 
    android:layout_centerHorizontal="true" 
    android:text="close" /> 

</RelativeLayout>