我创建了一个扩展DialogFragment
的类,它在onCreateDialog方法中返回一个AlertDialog
,如here。
问题是,我想增加标准(正)按钮的高度,但我无法控制它来改变它的高度。
当我做在的DialogFragment
Android 3.2 - 在DialogFragment中自定义AlertDialog按钮
mAlertDialog = new AlertDialog.Builder(getActivity())
.setView(messageView)
.setCustomTitle(titleView)
.setPositiveButton(R.string.dialog_button,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
((ContextSwapUserInterface) getActivity()).instructionsAccepted();
}
}
)
.create();
mAlertDialog.getButton(AlertDialog.BUTTON_POSITIVE).setHeight(60);
我得到一个例外,它说的onCreateDialog方法如下“......无法实例ComponentInfo ......”
我猜这是因为按钮在这个时间点没有正确实例化。
于是,我让我的主要活动按钮,我创建了DialogFragment
并把它称为是.show方法之后:
// Create and show a new InstructionsDialogFragment
DialogFragment instructionsDialogFragment = InstructionsDialogFragment.newInstance(mInstructions);
instructionsDialogFragment.show(getFragmentManager(), "Instructions Dialog");
((Button) instructionsDialogFragment.getDialog().findViewById(android.R.id.button1)).setHeight(60);
我也试过以下,而不是最后一行上面:
((AlertDialog) instructionsDialogFragment.getDialog()).getButton(AlertDialog.BUTTON_POSITIVE).setHeight(60);
这两个版本都会导致NullPointerException。在使用DialogFragment
时,有没有简单的方法可以自定义AlertDialog
的按钮?
我在onResume()方法中试过它,它工作,虽然你的建议可能也适用。不幸的是,我没有时间去测试它。坦克反正 – Schnodahipfe