具有可修改单行标题的 Android 对话框
简而言之,我正在开发一个“DurationPickerDialog”,其工作方式与 DatePickerDialog 的工作方式类似,但基于 xsd:duration 类型,因此用户指定年、月、日等数。
我还实现了一个“模糊持续时间”函数给我提供像“一个月前”这样的字符串形式的持续时间。我真的希望能够以与更新 DatePickerDialog 标题相同的方式更新 DurationPickerDialog 的标题,但似乎存在问题。在 DatePickerDialog 中,他们始终将其设置为单行,这样就不会“跳跃”。以下是 Android 源代码如何处理 DatePickerDialog 的标题。
// Note: before the skim-readers look at this bit, realize that this is NOT my
// code but Android's internal code for the DatePickerDialog.
@Override
public void show() {
super.show();
/* Sometimes the full month is displayed causing the title
* to be very long, in those cases ensure it doesn't wrap to
* 2 lines (as that looks jumpy) and ensure we ellipsize the end.
*/
TextView title = (TextView) findViewById(R.id.alertTitle);
title.setSingleLine();
title.setEllipsize(TruncateAt.END);
}
不幸的是,我无法访问他们的R.id.alertTitle
,因为它是com.android.internal.R
的一部分。
我见过像 这篇 stackoverflow 帖子 这样的实现它会让我修改 Window.FEATURE_CUSTOM_TITLE 属性,但这似乎不允许我(轻松)修改标题。
还有另一篇 stackoverflow 帖子提到了如何更改运行时在两个不同的 XML 布局之间添加标题,但这似乎也没有太大帮助,因为每次持续时间更改时都应该修改标题,并且为每个持续时间创建 XML 布局显然不是一个好主意。
那么,既然他们通过访问我们凡人无法访问的值来“作弊”,我是否可以通过另一种方式来做到这一点?
编辑:通过一些黑魔法,现在似乎它可以像我想要的那样省略文本?只是早些时候不是这样,现在我似乎无法重现这个问题。所以,我想当我们这样做的时候,有人可以向我解释一下我是如何实现这个魔法的吗?
In short, I'm working on a "DurationPickerDialog" that works similarly to how the DatePickerDialog works but works based on the xsd:duration type, so the user specifies the number of years, months, days, etc.
I've also implemented a "fuzzy duration" function that gives me durations as a string like "one month ago". I would really like to be able to update the DurationPickerDialog's title in the same manner that the DatePickerDialog's title is updated, but there seems to be a problem. In the DatePickerDialog, they have it set to be a single line all the time, so that it doesn't get "jumpy." Here's how Android's source does the DatePickerDialog's title.
// Note: before the skim-readers look at this bit, realize that this is NOT my
// code but Android's internal code for the DatePickerDialog.
@Override
public void show() {
super.show();
/* Sometimes the full month is displayed causing the title
* to be very long, in those cases ensure it doesn't wrap to
* 2 lines (as that looks jumpy) and ensure we ellipsize the end.
*/
TextView title = (TextView) findViewById(R.id.alertTitle);
title.setSingleLine();
title.setEllipsize(TruncateAt.END);
}
Unfortunately, I cannot access their R.id.alertTitle
, because it is part of com.android.internal.R
.
I have seen implementations like this stackoverflow post where it would have me modify the Window.FEATURE_CUSTOM_TITLE
attribute, but that doesn't seem to let me modify the title (easily) after that.
There was also another stackoverflow post that mentioned how to change the title at runtime between two different XML layouts, but that also doesn't seem like it would be all too helpful, since the title should be modified every time the duration changes, and creating an XML layout for each duration is obviously not a good idea.
So, since they "cheated" by accessing a value that us mere mortals don't have access to, is there another way that I could go about doing it?
Edit: And through some black magic, it seems that it now does ellipsize the text like I was wanting? Only earlier it wasn't, and now I can't seem to reproduce the problem. So, I suppose while we're at it, can someone explain to me how I might have accomplished this magic?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

我将通过扩展
Dialog
类并为其创建自定义 xml 布局来实现自定义对话框。您需要一些用于加/减和顶部/底部组合的自定义按钮背景以及一些按钮侦听器来操作值。
由于您要使用持续时间值,因此您可能需要比现有对话框提供的空间更多的空间。
这样您就可以将标题设置为您喜欢的任何内容。如果您需要代码示例,请告诉我。
例子:
对话框类:
在您的 Activity 中,您调用
this.showDialog(DURATION_DIALOG);
// DURATION_DIALOG 只是在 Activity 顶部指定的一个整数,用于标识下一段代码的对话框,其中处理实际创建对话框:最后,您可以像上面在
styles.xml
文件中那样设置对话框主题。例如:祝你好运!
I would implement a custom dialog by extending the
Dialog
class and creating a custom xml layout for it.You'd need some custom button backgrounds for the plus / minus and top / bottom combos and some button listeners to manipulate the values.
Since you are going for a duration value, you are probably going to need more space than the existing dialog gives you anyway.
This way you can set the title to whatever you like. Let me know if you need a code example.
Example:
The dialog class:
In your activity, you call
this.showDialog(DURATION_DIALOG);
// DURATION_DIALOG is just an integer specified at the top of your Activity to identify the dialog for the next peice of code, which handles actually creating the dialog:Finally, you can set your dialog theme as is done above in your
styles.xml
file. For example:Good luck!