[英]How to set font size for text of dialog buttons
我有一個 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);
您應該查看以下答案:
在 Dialog.java (Android src) 中使用了 ContextThemeWrapper。 所以你可以復制這個想法並做一些事情
您只需要更改以下代碼行:
<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);
}
}
我使用setOnShowListener嘗試了許多設備。 但它並不適用於所有設備。 最后,我決定最簡單的方法是為 alertDialog 使用主題。
將此添加到樣式文件中。
<style name="MyAlertDialogTheme" parent="Theme.AppCompat.Light.Dialog.Alert">
<item name="android:textSize">10sp</item>
</style>
現在在您的 AlertDialog 中使用它
val dialog = AlertDialog.Builder(requireActivity(),R.style.MyAlertDialogTheme)
就這樣。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.