据我所知,从AlertDialog和AlertController的代码中,您无法访问负责消息和标题的TextView.
您可以使用反射在AlertDialog实例中到达mAlert字段,然后再次使用反射来访问mAlert的mMessage和mTitle字段.虽然我不推荐这种方法,因为它依赖于内部(可能在将来发生变化).
作为另一个(可能更好)的解决方案,您可以通过AlertDialog构造函数应用自定义主题.这将允许您右对齐该对话框中的所有TextView.
protected AlertDialog (Context context, int theme)
这应该是更容易和更强大的方法.
这是一步一步的说明:
步骤1.创建res / values / styles.xml文件.其内容如下:
right|center_vertical
right|center_vertical
@style/RightJustifyTextView
@style/RightJustifyDialogWindowTitle
步骤2.创建RightJustifyAlertDialog.java文件.其内容如下:
public class RightJustifyAlertDialog extends AlertDialog
{
public RightJustifyAlertDialog(Context ctx)
{
super(ctx, R.style.RightJustifyTheme);
}
}
第3步.使用RightJustifyAlertDialog对话框:
AlertDialog dialog = new RightJustifyAlertDialog(this);
dialog.setButton("button", new OnClickListener()
{
public void onClick(DialogInterface arg0, int arg1)
{
}
});
dialog.setTitle("Some Title");
dialog.setMessage("Some message");
dialog.show();
第4步.检查结果: