如何使AlertDialog列表可滚动Android

时间:2018-04-29 05:54:51

标签: android listview android-studio android-alertdialog

我在AlertDialog中有一个List;

 public void thesaurusBtnAction(){
    // setup the alert builder
    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setTitle("Synonyms List");

    // add a list
    String[] someList = {"a", "b", "c", "d", "e","a","b"};
    builder.setItems(someList, new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            switch (which) {
                case 0: 
                case 1:
                case 2:
                case 3:
                case 4:
            }
        }
    });

    // create and show the alert dialog
    AlertDialog dialog = builder.create();
    dialog.show();
}

我想让这里的列表成为可滚动的列表,并使AlertDialog大小限制为一定的大小,因为随着列表的增长,AlertDialog会尝试增加它的大小。

2 个答案:

答案 0 :(得分:0)

将布局包含在ScrollView中。在随附的布局中,您可以将android:scrollbars设置为vertical。使用builder.setView在对话框中显示ScrollView。

答案 1 :(得分:0)

 public void thesaurusBtnAction(){
    // setup the alert builder
    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setTitle("Synonyms List");
    builder.setView(LayoutInflater.from(this).inflate(R.layout.dialoglayout,null));


    // create and show the alert dialog
    AlertDialog dialog = builder.create();
    dialog.show();
}
  

dialoglayout.xml

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:scrollbarAlwaysDrawVerticalTrack="true"
    android:scrollbars="vertical">
    <!--Any of your code-->
    <ListView
        android:layout_width="match_parent"
        android:layout_height="match_parent"></ListView>
</LinearLayout>