我创建了一个扩展DialogFragment的类,它在onCreateDialog方法中返回一个AlertDialog,就像here一样。
问题是,我想增加标准(正)按钮的高度,但我无法控制它来改变它的高度。
当我在DialogFragment的onCreateDialog方法中执行以下操作时
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);我收到一个异常,它说“...无法实例化ComponentInfo ...”
我想这是因为Button在这个时候没有正确实例化
所以我在创建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的Button?