以编程方式将控件添加到自定义对话框 [英] Add controls to custom dialog programmatically

查看:87
本文介绍了以编程方式将控件添加到自定义对话框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想显示一个带有〜50个自定义控件(开关按钮)的对话框.因此,最好的方法是以编程方式将它们添加到循环中.我试图制作一个dilog,布局只包含一个GroupView元素:

I want to show a dialog with ~50 custom controls (switch buttons) on it. So, the best way to do that is to add them programmatically in a loop. I've tried to make a dilog with a layout wich contains the only one GroupView element:

<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout
    android:layout_height="match_parent"
    android:layout_width="match_parent"
    android:orientation="vertical"
    android:background="#AAAAAA"
    xmlns:android="http://schemas.android.com/apk/res/android">
    <ViewGroup
        android:layout_height="500dp"
        android:layout_width="500dp"
        android:id="@+id/dlg_view"/>
</LinearLayout>

,然后使用onCreateDialog(...)方法在其中添加我的控件:

and then just add my controls in it using: onCreateDialog(...) method:

AlertDialog.Builder builder = new AlertDialog.Builder(this);
LayoutInflater inflater = getLayoutInflater();
builder.setView(inflater.inflate(R.layout.geomap_menu, null))
          .setPositiveButton("Ok", new DialogInterface.OnClickListener() {
              @Override
              public void onClick(DialogInterface dialog, int id) {
                 // sign in the user ...
              }
          })
          .setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
              public void onClick(DialogInterface dialog, int id) {
                  //LoginDialogFragment.this.getDialog().cancel();
              }
});

Dialog res = builder.create();
ViewGroup dlgView = (ViewGroup)res.findViewById(R.id.dlg_view);
MyControl myControl = new MyControl(this);
dlgView.add(myControl);

但是它不能以这种方式工作(它抛出InflateException).我在做什么错了?

But it doesn't work this way (it throws InflateException). What am I doing wrong?

推荐答案

代码中的问题非常明显:

The problems in your code were pretty obvious:

  • 在布局文件中使用ViewGroup,它是一个抽象类(Android中所有布局的根),无法实例化,因此很可能是您所说的膨胀异常的原因关于.使用ViewGroup的子类之一,例如LinearLayoutRelativeLayout等,更适合您.

  • In your layout file you use ViewGroup which is an abstract class(the root of all layouts in Android) and which can't be instantiated so it will most likely be the reason for that inflate exception you talk about. Use one of the subclasses of ViewGroup, like LinearLayout, RelativeLayout etc, which one fits you better.

即使进行了我在您的代码上方所写的修改,它仍然可以自动执行.首先,ViewGroup类没有add方法,您可能是指addView方法之一.第二个dlgView将是null,因为那时没有显示Dialog,所以没有View可以找到.您可以通过在其中一个视图上发布Runnable来延迟设置视图,直到显示Dialog来完成此操作:

Even after doing the modification I wrote above your code will still bot work. First the ViewGroup class doesn't have an add method, you're probably referring to one of the addView methods. Second the dlgView will be null because at that moment the Dialog isn't displayed so there is no View to find. You can do it by posting a Runnable on one of your views to delay setting the views until the Dialog is shown:

final Dialog res = builder.create();
oneOfYourViews.post(new Runnable() {

    @Override
    public void run() {
        ViewGroup dlgView = (ViewGroup) res.findViewById(R.id.dlg_view);
        MyControl myControl = new MyControl(context);
        dlgView.addView(myControl);             
    }
});

代码添加:

View contentView = inflater.inflate(R.layout.geomap_menu, null)
ViewGroup dlgView = (ViewGroup) contentView.findViewById(R.id.dlg_view);
MyControl myControl = new MyControl(this);
dlgView.addView(myControl); // or add the other views in the loop as many as you want
builder.setView(contentView);
// rest of your code

这篇关于以编程方式将控件添加到自定义对话框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆