【问题标题】:How to make a AlertDialog List scrollable Android [duplicate]如何使AlertDialog List可滚动Android [重复]
【发布时间】:2018-10-09 13:30:03
【问题描述】:

我在 AlertDialog 中有一个列表;

 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 会随着列表的增长尝试增加其大小。

【问题讨论】:

  • 我很糟糕,它是上述问题的副本。我认为如果我只是使用 setLayout() 调整大小,它将无法滚动。我会将其标记为重复

标签: android listview android-studio android-alertdialog


【解决方案1】:
 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>

【讨论】:

  • 问题是关于AlertDialog 的默认列表实现,而不是自定义视图
  • 感谢您的回答,但正如@ADM 所说,这是关于默认列表实现的。
【解决方案2】:

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

【讨论】:

  • 我认为问题是关于 AlertDialog 的默认列表实现而不是自定义视图。
  • 感谢您的回答,但正如@ADM 所说,这是关于默认列表实现
猜你喜欢
  • 1970-01-01
  • 2011-08-31
  • 1970-01-01
  • 2016-11-06
  • 2020-01-11
  • 2022-01-09
  • 1970-01-01
  • 2012-11-20
  • 1970-01-01
相关资源
最近更新 更多