【问题标题】:How to set font size for text of dialog buttons如何设置对话框按钮文本的字体大小
【发布时间】:2013-04-01 08:08:55
【问题描述】:

我有一个 android 应用程序,它使用一些从 XML 布局扩展而来的自定义对话框。对话框视图的内容来自 XML 布局,但实际的正负按钮是通过调用构建器的 setPositiveButton 和 setNegativeButton 方法添加的,所以我无法控制(或至少不知道如何控制)样式按钮本身。

从我的 LoginConfirmationDialog.java 文件中查看下面的 onCreateDialog 方法,该文件扩展了 DialogFragment。它基本上会弹出一个非常简单的对话框,要求确认谁在登录(即“你是 Joe Schmoe 吗?”,带有是和否按钮)。

在这种情况下,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();    
}

【问题讨论】:

  • 既然您已经在为对话框使用 xml 文件,为什么不在布局中包含两个按钮并在对话框创建中设置 onClick 处理程序?
  • 我想这是一个很好的观点。我有点喜欢使用默认按钮来获得外观和感觉,但我想我可以将按钮直接添加到布局中并在它们上设置侦听器。
  • @Joseph Selvaraj - 感谢您的链接,这看起来也是一个不错的解决方案。给猫剥皮的方法有很多。

标签: java android android-dialogfragment


【解决方案1】:

不要返回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;

【讨论】:

    【解决方案2】:

    你应该看看下面的答案:

    In Dialog.java (Android src) a ContextThemeWrapper is used. So you could copy the idea and do something

    您只需更改以下代码行:

    <item name="android:textSize">10sp</item> 到您想要的大小。

    并且不要忘记检查答案的 cmets。

    祝你好运。

    【讨论】:

      【解决方案3】:

      既然您已经在为对话框使用 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);
      

      【讨论】:

      • +1。这也是一个很好的答案。对于我正在处理的一个特定案例,我选择了 ssantos 的解决方案,但将来我也可能会使用您的解决方案;特别是如果我希望按钮的排列方式与默认对话框按钮不同。
      【解决方案4】:

      花了我一段时间来整合 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 不同,它与设备密度无关。

      【讨论】:

        【解决方案5】:

        我的方法是获取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);
          }
        }
        

        【讨论】:

          【解决方案6】:

          你可以试试这个:

          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);
          

          【讨论】:

            【解决方案7】:

            我使用 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)
            

            就是这样。

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 1970-01-01
              • 2014-03-25
              • 2022-08-06
              • 2013-08-11
              • 1970-01-01
              • 2012-08-08
              • 1970-01-01
              • 2019-06-27
              相关资源
              最近更新 更多