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

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

问题描述

我想显示同〜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 programatically 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 I'm doing wrong?

我希望有人可以踹我在正确的方向...

I'm hoping someone can kick me in the right direction...

推荐答案

在code中的问题是pretty的显而易见的:

The problems in your code were pretty obvious:

  • 在布局文件中使用的ViewGroup 这是一个抽象类(在Android的所有布局的根),并因此不能被实例化它会最有可能的是,你谈的充气异常的原因。使用的ViewGroup 的子类,例如的LinearLayout RelativeLayout的等,哪个最适合你更好。

  • 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.

即使这样做,我写上面的code仍将机器人工作改性后。首先,的ViewGroup 类没有一个添加方法,你可能指的是一个 addView 的方法。第二 dlgView ,因为在那一刻对话框没有显示,所以没有查看找到。您可以在您的意见延迟设置,直到对话框显示在视图中的一个张贴的Runnable 做到这一点:

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);             
    }
});

code另外:

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天全站免登陆