在AlertDialog右对齐文本 [英] Right justify text in AlertDialog

查看:486
本文介绍了在AlertDialog右对齐文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有可能右对齐在AlertDialog的标题和留言的文字?

Is it possible to right-justify the text in an AlertDialog's title and message?

我显示希伯来文,但他们表现出了左对齐。

I am showing Hebrew messages but they are showing up left justified.

推荐答案

据我可以从<一的code见href="http://$c$csearch.google.com/$c$csearch/p?hl=en#uX1GffpyOZk/core/java/android/app/AlertDialog.java&q=package%3aandroid.git.kernel.org%20file%3aandroid/app/AlertDialog.java"相对=nofollow> AlertDialog 和<一href="http://$c$csearch.google.com/$c$csearch/p?hl=en#uX1GffpyOZk/core/java/com/android/internal/app/AlertController.java&q=package%3aandroid.git.kernel.org%20file%3aandroid/app/AlertDialog.java"相对=nofollow> AlertController 您不能访问的TextView 取值负责信息和标题。

As far as I can see from the code of AlertDialog and AlertController you can't access the TextViews responsible for message and title.

您可以使用反射在AlertDialog情况下达到 mAlert 字段,然后再次使用反射来访问 mMessage mTitle mAlert 的领域。虽然我不会推荐这种方法,因为它依赖于内部(这可能会在未来改变)。

You can use reflection to reach mAlert field in AlertDialog instance, and then again use reflection to access mMessage and mTitle fields of mAlert. Though I wouldn't recommend this approach as it relies on internals (which might change in future).

另一个(可能更好)的解决方案,您可以通过 AlertDialog 构造应用自定义主题。这将允许你右对齐所有的TextView S在该对话框。

As another (and probably much better) solution, you can apply custom theme via AlertDialog constructor. This would allow you to right-justify all TextViews in that dialog.

     protected AlertDialog (Context context, int theme)

这应该是更容易和更可靠的方法。

This should be easier and more robust approach.

下面是一步一步的指示:

Here is step by step instructions:

第1步创建 RES /价值/ styles.xml 文件。在这里,它的内容:

Step 1. Create res/values/styles.xml file. Here its content:

<?xml version="1.0" encoding="utf-8"?>
<resources>

    <style name="RightJustifyTextView" parent="@android:style/Widget.TextView">
        <item name="android:gravity">right|center_vertical</item>
    </style>

    <style name="RightJustifyDialogWindowTitle" parent="@android:style/DialogWindowTitle" >
         <item name="android:gravity">right|center_vertical</item>
    </style>

    <style name="RightJustifyTheme" parent="@android:style/Theme.Dialog.Alert">
        <item name="android:textViewStyle">@style/RightJustifyTextView</item>       
        <item name="android:windowTitleStyle">@style/RightJustifyDialogWindowTitle</item>       
    </style>    

</resources>

第2步创建 RightJustifyAlertDialog.java 文件。在这里,它的内容:

Step 2. Create RightJustifyAlertDialog.java file. Here its content:

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

第四步查看结果:

这篇关于在AlertDialog右对齐文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆