在 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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
这是一个老问题,但有一个非常简单的解决方案。假设您使用的是
MinSdk 17
,您可以将其添加到您的styles.xml
中:并且在
AlertDialog.Builder
中您只需指定此内容构造函数中的AlertDialogCustom
:This is an old question, but there is a very simple solution. Assuming you are using
MinSdk 17
, you can add this to yourstyles.xml
:And in the
AlertDialog.Builder
you just need to specify thisAlertDialogCustom
in the constructor:据我从
AlertDialog
和AlertController
您无法访问负责消息的TextView
标题。您可以使用反射来访问 AlertDialog 实例中的
mAlert
字段,然后再次使用反射来访问mAlert< 的
mMessage
和mTitle
字段/代码>。尽管我不推荐这种方法,因为它依赖于内部结构(将来可能会改变)。作为另一个(可能更好)的解决方案,您可以通过
AlertDialog
构造函数应用自定义主题。这将允许您右对齐该对话框中的所有TextView
。这应该是更简单、更稳健的方法。
以下是分步说明:
第 1 步。创建
res/values/styles.xml
文件。其内容如下:第 2 步。创建
RightJustifyAlertDialog.java
文件。其内容如下:第 3 步。 使用
RightJustifyAlertDialog
对话框:第 4 步。 检查结果:
As far as I can see from the code of
AlertDialog
andAlertController
you can't access theTextView
s responsible for message and title.You can use reflection to reach
mAlert
field in AlertDialog instance, and then again use reflection to accessmMessage
andmTitle
fields ofmAlert
. Though I wouldn't recommend this approach as it relies on internals (which might change in future).As another (and probably much better) solution, you can apply custom theme via
AlertDialog
constructor. This would allow you to right-justify allTextView
s in that dialog.This should be easier and more robust approach.
Here is step by step instructions:
Step 1. Create
res/values/styles.xml
file. Here its content:Step 2. Create
RightJustifyAlertDialog.java
file. Here its content:Step 3. Use
RightJustifyAlertDialog
dialog:Step 4. Check the results:
如果您想要一种快速且简单的方法来执行此操作,我只需使用 AlertDialog.Builder 创建和修改默认警报。您甚至可以创建一个方便的方法和类,如下所示:
当然,您也可以将重力更改为 CENTER 以进行中心对齐,而不是右对齐或默认的左对齐。
If you want a quick and easy way to do this, I would just create and modify the default alert using AlertDialog.Builder. You can even create a convenience method and class like so:
Of course you could also change the gravity to CENTER for center alignment instead of right or the default left.
经过一个小时的努力(上面的解决方案对我不起作用),我设法将 AlertDialog 的标题和消息向右对齐,而不覆盖任何样式,只需更改重力和布局参数即可。
在DialogFragment中:
实际功能:
After struggling with this for an hour (the solutions above didn't work for me), I managed to align the AlertDialog's title and message to the right, without overriding any styles, simply changing the gravity and layout params.
In DialogFragment:
The actual function:
kotlinish 方式:
The kotlinish way :
您所需要做的就是在 res/values/styles 中创建一个样式标记并写入:
,然后在活动中初始化警报对话框后添加此标记:
例如:
All you need is to create a style tag in res/values/styles and write :
and then add this after alert dialog initializing in your activity :
for example :
要将警报对话框的布局方向设置为 RTL,您可以使用 OnShowListener 方法。
设置 title , message , .... 之后使用此方法。
For setting layout direction of alert dialog to RTL you can use OnShowListener method.
after setting title , message , .... use this method.