java 如何设置对话框按钮文本的字体大小
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15909672/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to set font size for text of dialog buttons
提问by Jim
I have an android app that uses some custom dialogs which are inflated from XML layouts. The contents of the dialog's view come from the XML layout, but the actual positive and negative buttons are added by calling the builder's setPositiveButton and setNegativeButton methods, so I have no control over (or at least don't know how to control) the styling of the buttons themselves.
我有一个 android 应用程序,它使用一些从 XML 布局膨胀的自定义对话框。对话框视图的内容来自 XML 布局,但实际的正负按钮是通过调用构建器的 setPositiveButton 和 setNegativeButton 方法添加的,因此我无法控制(或至少不知道如何控制)样式按钮本身。
See the onCreateDialog method below from my LoginConfirmationDialog.java file which extends DialogFragment. It basically pops a very simple dialog up that asks for confirmation of who is logging in (i.e. "Are you Joe Schmoe?", with Yes and No buttons).
从我的 LoginConfirmationDialog.java 文件中查看下面的 onCreateDialog 方法,它扩展了 DialogFragment。它基本上会弹出一个非常简单的对话框,要求确认谁正在登录(即“你是 Joe Schmoe 吗?”,带有 Yes 和 No 按钮)。
The XML layout in this case has just a single TextView, and to make this easy (because the users will be construction workers with big knobby dirty fingers who need large text and large buttons), I made the font for the TextView pretty big. The two buttons though have much smaller font for their text, and since they aren't part of my layout and are added with the setPositiveButton and setNegativeButton methods, how do I control the font size?
在这种情况下,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();
}
回答by ssantos
Instead of returning builder.create()
, try this.-
与其返回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;
回答by TomV
Took me a while, to integrate Asok's answer, since I used anonymous inner classes for buttons, so I needed to get a handle on the button references. This works. Make sure it goes after the messageDialog.show() line:
我花了一段时间来整合 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);
Note: It's recommended to use sp as a unit for text size. Unlike px, it is device density independent.
注意:建议使用 sp 作为文本大小的单位。与 px 不同,它与设备密度无关。
回答by jnthnjns
Since you are already using an xml file for the dialog why not just include the two buttons in the layout and set the onClick
handlers in the dialog creation, something like this should work. I am using something similar.
既然您已经在对话框中使用 xml 文件,为什么不只在布局中包含两个按钮并onClick
在对话框创建中设置处理程序,这样的事情应该可以工作。我正在使用类似的东西。
Here is a quick example:
这是一个快速示例:
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();
I am also quite fond of using the following for setting text sizes, this allows for various screen sizes to get a different size of text (You can play with the float
value to suit your needs):
我也非常喜欢使用以下设置文本大小,这允许各种屏幕尺寸获得不同大小的文本(您可以float
根据需要使用该值):
.setTextSize(TypedValue.COMPLEX_UNIT_PX, (float) 25);
回答by Shajeel Afzal
You should check out the following answer:
您应该查看以下答案:
在 Dialog.java (Android src) 中使用了 ContextThemeWrapper。所以你可以复制这个想法并做一些事情
You just have to change the following line of code:
您只需要更改以下代码行:
<item name="android:textSize">10sp</item>
to your desired size.
<item name="android:textSize">10sp</item>
到您想要的尺寸。
And don't forget to check the comments of the answer also.
并且不要忘记检查答案的评论。
Best of luck.
祝你好运。
回答by Dino Tw
My approach is to obtain the buttons in onResume()
and configure them there
我的方法是获取按钮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);
}
}
回答by SO 80
You can try this:
你可以试试这个:
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);