ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

android – 在AlertDialog中右对齐文本

2019-09-27 10:23:58  阅读:356  来源: 互联网

标签:android-alertdialog android justify


是否可以在AlertDialog的标题和消息中对文本进行右对齐?

我正在展示希伯来语的消息,但它们显示左对齐.

解决方法:

据我所知,从AlertDialogAlertController的代码中,您无法访问负责消息和标题的TextView.

您可以使用反射在AlertDialog实例中到达mAlert字段,然后再次使用反射来访问mAlert的mMessage和mTitle字段.虽然我不推荐这种方法,因为它依赖于内部(可能在将来发生变化).

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

     protected AlertDialog (Context context, int theme)

这应该是更容易和更强大的方法.

这是一步一步的说明:

步骤1.创建res / values / styles.xml文件.其内容如下:

<?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文件.其内容如下:

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步.检查结果:

标签:android-alertdialog,android,justify
来源: https://codeday.me/bug/20190927/1823570.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有