更改AlertDialog按钮排列 [英] Changing AlertDialog buttons alignment

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

问题描述

在争取时间有关更改内部AlertDialog(support.v7一个)按钮的排列,由于根据现场观察方向,他们不会赞同,尽管整个应用程序做左对齐,也在AlertDialog中的文本。

Been fighting for hours about changing the alignment of the buttons inside AlertDialog (support.v7 one), since they won't align themselves according to the locale view direction, despite the whole app DOES align to left and also the text inside the AlertDialog.

(为什么会出现这种情况,你说什么?我编程配置区域设置语言是恩,因为那是我的默认应用程序的语言,即使系统区域可能是别的东西)。

(Why would this happen you say? I'm programatically configuring the locale language to be "en" since that's my default app language, even though the system locale might be something else).

所以,就像我说的,我不需要触碰消息的对话框里面,但作为一个例子,这是怎么改的方向:

So like I said, I don't need to touch the message inside the dialog, but as an example, that's how to change it's direction:

TextView messageView = (TextView)dialog.findViewById(android.R.id.message);
messageView.setGravity(Gravity.RIGHT); // or LEFT

当然,它不会对按钮的工作,因为我需要改变布局重心代替。

Of course it doesn't work on the buttons, since I need to change the layout gravity instead.

下面是我如何找到按钮(我称之为秀()在课程的AlertDialog.Builder后,否则会为空):

Here's how I find the buttons (after I call show() on the AlertDialog.Builder of course, else they would be null):

AppCompatButton accept = (AppCompatButton)dialog.findViewById(android.R.id.button1);
AppCompatButton cancel = (AppCompatButton)dialog.findViewById(android.R.id.button2);

和这里就是我试图改变他们的父母的LinearLayout内的对齐方式:

And here's how I attempt to change their alignment inside their parent LinearLayout:

((LinearLayout.LayoutParams)accept.getLayoutParams).gravity = Gravity.RIGHT;
((LinearLayout.LayoutParams)cancel.getLayoutParams).gravity = Gravity.RIGHT;

我选择权,因为该对话框内的按钮的一侧总是相反其文本对齐一边。(是的 - 我想左也没有什么变化)

I chose RIGHT since the side of the buttons inside the dialog is always opposite to the side which the text is aligned to. (Yes - I tried LEFT also, nothing changed).

这是行不通的。有没有人有一个想法如何实现这一目标?看来他们只是坚持着自己的地方。

This doesn't work. Does anyone have an idea how to achieve this? It seems they are just stuck to their place.

编辑: 标题是不是也赞同,我只是证实了这一点(因为它出现在右边,就像我的系统配置,而不​​是像我的语言环境配置的一些原因)。

Title isn't aligned also, I just confirmed this (for some reason it appears on the right, like my system configuration, and not like my locale configuration).

推荐答案

您可以完全自主的AlertDialog。 这里的窍门是可能使用自定义视图对话框,并创建自己的按钮,在视图中。

You can completely customize an AlertDialog. The trick here is probably to use a custom view for the dialog, and create your own buttons in that view.

有关示例,请参阅 <一href="http://stackoverflow.com/questions/18352324/how-can-can-i-add-custom-buttons-into-an-alertdialogs-layout#answer-18353260">How可我可以添加自定义按钮到AlertDialog的布局?

这篇关于更改AlertDialog按钮排列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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