2013-04-09 68 views
7

我有一个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();  
} 
+0

为什么不直接包括在布局中的两个按钮并设置'onClick'在对话框创建处理程序? – jnthnjns 2013-04-09 18:51:58

+0

我想这是一个很好的观点。我很喜欢使用默认按钮来获得它们的外观和感觉,但我想我可以直接将按钮添加到布局并在其上设置监听器。 – Jim 2013-04-09 18:58:40

+1

另一种方法是在该问题提供 [http://stackoverflow.com/questions/8881710/how-to-reduce-alertdialog-builder-title-font-size-and-positive-button-size] [ 1] [1]:http://stackoverflow.com/questions/8881710/how-to-reduce-alertdialog-builder-title-font-size-and-positive-button-size – 2013-04-09 18:59:47

回答

28

,而不是返回builder.create(),尽量this.-

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

+1。这也是一个很好的答案。对于我正在研究的这个特定案例,我采用了ssantos的解决方案,但将来我也可能会使用您的解决方案;特别是如果我想按钮的排列方式与默认对话框按钮不同。 – Jim 2013-04-10 14:35:29

4

我花了一段,整合奥绍克的答案,因为我用的按钮匿名内部类,所以我需要获取按钮引用的句柄。这工作。请确保它那张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不同,它与设备密度无关。

0

我的做法是获得onResume()按钮和既然你已经使用XML文件的对话框配置它们有

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); 
    } 
} 
相关问题