这是我的布局
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<net.simonvt.widget.NumberPicker
android:id="@+id/numberPicker"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="100dp" >
</net.simonvt.widget.NumberPicker>
</RelativeLayout>
它在我的应用中显示了一个自定义选择器。但是我想在对话框中显示这个选择器。
这是我的自定义对话框:
public class NumberPickerCustomDialog extends DialogFragment {
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setMessage("Dialog")
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
}
})
.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
}
}).;
return builder.create();
}
如何将我的选择器放入此对话框?
谢谢!
答案 0 :(得分:3)
您需要为对话框提供自定义布局,以便抓取LayoutInflater服务并使用它来扩充您的布局。
public class NumberPickerCustomDialog extends DialogFragment {
Context context;
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
// get context
context = getActivity().getApplicationContext();
// make dialog object
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
// get the layout inflater
LayoutInflater li = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
// inflate our custom layout for the dialog to a View
View view = li.inflate(R.layout.my_custom_view, null);
// inform the dialog it has a custom View
builder.setView(view);
// and if you need to call some method of the class
MyCustomView myView = (MyCustomView) view.findViewById(R.id.custom_id_in_my_custom_view);
myView.doSome("stuff");
// create the dialog from the builder then show
return builder.create();
}
}