我正在尝试创建一个AlertDialog,其中包含“下一个”和“关闭”按钮以及“不再显示”的复选框。我使用DialogFragment的支持库。以下代码工作正常,但我想使用我自己的xml布局为此AlertDialog:
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
int title = getArguments().getInt("num");
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setTitle("ASDASDAS")
.setPositiveButton(R.string.hello_world,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
((MainActivity)getActivity()).doPositiveClick();
}
}
)
.setNegativeButton(R.string.cancel,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
((MainActivity)getActivity()).doNegativeClick();
}
}
);
return builder.create();
}
是否可以使用我自己的xml布局来创建此AlertDialog?
答案 0 :(得分:4)
这是使用您自己的xml布局在DialogFragment中创建完全自定义AlertDialog的方法。
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
// Get the layout inflater
LayoutInflater inflater = getActivity().getLayoutInflater();
// Inflate and set the layout for the dialog
// Pass null as the parent view because its going in the dialog layout
builder.setView(inflater.inflate(R.layout.dialog_signin, null))
// Add action buttons
.setPositiveButton(R.string.signin, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int id) {
// sign in the user ...
}
})
.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
LoginDialogFragment.this.getDialog().cancel();
}
});
return builder.create();
答案 1 :(得分:1)
答案 2 :(得分:1)
您可以像这样使用Dialog:
private void showIconsDlg(final int btnId) {
// Use a custom style: IconsDialog
final Dialog dlg = new Dialog(mContext, R.style.IconsDialog);
// Use a custom layout:
dlg.setContentView(R.layout.your_custom_dlg);
// Find and init Views
GridView grid = (GridView) dlg.findViewById(R.id.icon_grid);
grid.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
...
}
});
dlg.show();
}
这是主题:IconsDialog:
<style name="IconsDialog" parent="@android:style/Theme.Dialog">
<item name="android:backgroundDimEnabled">true</item>
<item name="android:windowBackground">@drawable/icons_dlg_bg</item>
<item name="android:windowNoTitle">true</item>
</style>
在R.layout.your_custom_dlg。
中设置自定义提醒布局