【问题标题】:Android scrollable AlertDialog programmaticallyAndroid 可滚动的 AlertDialog 以编程方式
【发布时间】:2014-05-28 14:16:29
【问题描述】:

如何以编程方式使我的 AlertDialog 可滚动?

我的 AlertDialog 是这样定义的:

private void DisplayAlertChoice(String title, String msg, final int pos)
{       
    alert = new AlertDialog.Builder(this);
    alert.setTitle(title);
    final LinearLayout layout = new LinearLayout(this);
    layout.setOrientation(LinearLayout.VERTICAL);
     CheckBox[] t=new CheckBox[15];
     for (i=0;i<currentBattery.temperatureNumber;i++)
     {               
         t[i]=new CheckBox(this);
         t[i].setText("title");
         t[i].setChecked(false);
         t[i].setOnCheckedChangeListener(this);

         layout.addView(t[i]);      
     }
    alert.setView(layout);      
    alert.show();   

}

我尝试了一些在网上找到的解决方案,但对我不起作用。

谁能给我一个相关的解决方案,让它以编程方式滚动?

【问题讨论】:

  • 你试过把 LinearLayout 放到 ScrollView 里吗?
  • 你可能还想看看multi-choice mode
  • 多选很酷@codeMagic
  • @blackbelt :我试过了,但它迫使我的应用停止。
  • 使用 ScrollView 应该可以工作。请发布您获得的堆栈跟踪。

标签: java android layout android-alertdialog programmatically-created


【解决方案1】:

我试过了,但它会强制我的应用停止。

ScrollView 可以只有一个孩子。这是一个Android约束。所以,做

    final LinearLayout layout = new LinearLayout(this);
    ScrollView scrollView = new ScrollView(this);
    layout.setOrientation(LinearLayout.VERTICAL);
     CheckBox[] t=new CheckBox[15];
     for (i=0;i<currentBattery.temperatureNumber;i++)
     {               
         t[i]=new CheckBox(this);
         t[i].setText("title");
         t[i].setChecked(false);
         t[i].setOnCheckedChangeListener(this);

         scrollView.addView(t[i]);      
     }

会使您的应用程序崩溃,因为您向 ScrollView 添加了多个子项。但是如果你这样做了

final LinearLayout layout = new LinearLayout(this);
layout.setOrientation(LinearLayout.VERTICAL);
 CheckBox[] t=new CheckBox[15];
 for (i=0;i<currentBattery.temperatureNumber;i++)
 {               
     t[i]=new CheckBox(this);
     t[i].setText("title");
     t[i].setChecked(false);
     t[i].setOnCheckedChangeListener(this);

     layout.addView(t[i]);      
 }
 final ScrollView scrollView = new ScrollView(this); 
 scrollView.addView(layout);

它应该可以顺利运行。

【讨论】:

  • 谢谢它的工作。我的错误是将“布局”而不是“滚动视图”添加到我的警报对象。 alert.setView(layout);
  • 这根本不适合我
  • 8 年前它起作用了。 API 变化很大
猜你喜欢
  • 2015-11-14
  • 1970-01-01
  • 1970-01-01
  • 2011-09-20
  • 2011-01-15
  • 2015-10-05
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多