将控件添加到自定义对话框编程
我想显示同〜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 ofViewGroup
, likeLinearLayout
,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