我有一个Android应用程序,它使用一些从XML布局膨胀的自定义对话框。对话框视图的内容来自XML布局,但实际的正面和负面按钮是通过调用构建器的setPositiveButton和setNegativeButton方法添加的,因此我无法控制(或至少不知道如何控制)样式按钮本身。
请参阅我的LoginConfirmationDialog.java文件中的onCreateDialog方法,该文件扩展了DialogFragment。它基本上弹出一个非常简单的对话框,要求确认谁登录(即“你是Joe Schmoe吗?”,带有Yes和No按钮)。
在这种情况下,XML布局只有一个TextView,为了简化这一点(因为用户将是需要大文本和大按钮的大手指脏手指的建筑工人),我使TextView的字体非常大。这两个按钮的文字字体要小得多,因为它们不是我的布局的一部分,并且添加了setPositiveButton和setNegativeButton方法,我该如何控制字体大小?
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
Bundle args = this.getArguments();
String empName = args.getString("empName");
// Use the Builder class for convenient dialog construction
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
View view = getActivity().getLayoutInflater().inflate(R.layout.dialog_login_confirmation, null);
TextView message = (TextView)view.findViewById(R.id.txtLoginConfirmationMessage);
message.setText("Are you " + empName + "?");
builder.setView(view);
builder.setPositiveButton("Yes",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
mListener.onEmpConfirmPositiveClick(LoginConfirmationDialog.this);
}
});
builder.setNegativeButton("No",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
mListener.onEmpConfirmNegativeClick(LoginConfirmationDialog.this);
}
});
// Create the AlertDialog object and return it
return builder.create();
}
试试这个,而不是返回builder.create()
.-
final AlertDialog alert = builder.create();
alert.setOnShowListener(new DialogInterface.OnShowListener() {
@Override
public void onShow(DialogInterface dialog) {
Button btnPositive = alert.getButton(Dialog.BUTTON_POSITIVE);
btnPositive.setTextSize(TEXT_SIZE);
Button btnNegative = alert.getButton(Dialog.BUTTON_NEGATIVE);
btnNegative.setTextSize(TEXT_SIZE);
}
});
return alert;
花了一段时间,整合Asok的答案,因为我使用了按钮的匿名内部类,所以我需要处理按钮引用。这有效。确保它在messageDialog.show()行之后:
messageDialog.show();
messageDialog.getButton(AlertDialog.BUTTON_POSITIVE).setTextSize(TypedValue.COMPLEX_UNIT_SP, 25.0f);
messageDialog.getButton(AlertDialog.BUTTON_NEUTRAL).setTextSize(TypedValue.COMPLEX_UNIT_SP, 25.0f);
注意:建议使用sp作为文本大小的单位。与px不同,它与设备密度无关。
由于您已经在对话框中使用xml文件,为什么不只是在布局中包含两个按钮并在对话框创建中设置onClick
处理程序,这样的事情应该可行。我正在使用类似的东西。
这是一个简单的例子:
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
View view = getActivity().getLayoutInflater().inflate(R.layout.dialog_login_confirmation, null);
TextView message = (TextView)view.findViewById(R.id.txtLoginConfirmationMessage);
message.setText("Are you " + empName + "?");
Button positiveBtn = (Button) view.findViewById(R.id.dialogButtonPositive);
// Set size of button in relation to screen size
positiveBtn.setTextSize(TypedValue.COMPLEX_UNIT_PX, (float) 25);
positiveBtn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
mListener.onEmpConfirmPositiveClick(LoginConfirmationDialog.this);
}
});
Button negativeBtn = (Button) view.findViewById(R.id.dialogButtonNeg);
// Set size of button in relation to screen size
negativeBtn.setTextSize(TypedValue.COMPLEX_UNIT_PX, (float) 25);
negativeBtn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
mListener.onEmpConfirmNegativeClick(LoginConfirmationDialog.this);
}
});
builder.setView(view);
return builder.create();
我也非常喜欢使用以下设置文本大小,这允许各种屏幕大小来获得不同大小的文本(您可以使用float
值来满足您的需求):
.setTextSize(TypedValue.COMPLEX_UNIT_PX, (float) 25);
你应该看看以下答案:
您只需更改以下代码行:
<item name="android:textSize">10sp</item>
到你想要的大小。
并且不要忘记检查答案的评论。
祝你好运。
你可以试试这个:
AlertDialog.Builder builder = new AlertDialog.Builder(CompQuestionsActivity.this);
builder.setMessage("Message");
builder.setPositiveButton("Yes", dialogClickListener);
builder.setNegativeButton("No", dialogClickListener);
AlertDialog alertDialog = builder.create();
alertDialog.show();
//For positive button:
Button button1 = alertDialog.findViewById(android.R.id.button1);
button1.setTextSize(25);
//For negative button:
Button button2 = alertDialog.findViewById(android.R.id.button2);
button2.setTextSize(25);
我的方法是获取onResume()
中的按钮并在那里配置它们
public class LoginConfirmationDialog extends DialogFragment {
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
// your code here remains unchanged
}
@Override
public void onResume() {
super.onResume();
Button positiveButton = ((AlertDialog) getDialog()).getButton(AlertDialog.BUTTON_POSITIVE);
positiveButton.setTextSize(TypedValue.COMPLEX_UNIT_SP, 20);
Button negativeButton = ((AlertDialog) getDialog()).getButton(AlertDialog.BUTTON_NEGATIVE);
negativeButton.setTextSize(TypedValue.COMPLEX_UNIT_SP, 20);
}
}