具有标准图标、标题和按钮的独立于平台版本的自定义对话框

发布于 2024-12-21 20:47:49 字数 1696 浏览 6 评论 0原文

我想要归档的内容:我想要一个带有自定义视图的对话框,但我想要 AlertDialog 中的标准图标、标题和按钮。

我正在做的是这个自定义对话框类:

public class CustomDialog extends AlertDialog.Builder {

    private Activity activity;
    private View root;

    public CustomDialog(Activity context) {
        super(context);
        this.activity = context;
    }

    public void setView(int layoutResID) {
        LayoutInflater inflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        root = inflater.inflate(layoutResID, (ViewGroup) activity.findViewById(R.id.dialog_root), false);
        ScrollView scroller = new ScrollView(activity);
        scroller.addView(root);
        setView(scroller);
    }

    public void setCustomView(View v) {
        ScrollView scroller = new ScrollView(activity);
        scroller.addView(v);
        setView(scroller);
    }

    public View getRoot() {
        return root;
    }

    @Override
    public AlertDialog create() {
        AlertDialog dialog = super.create();

        dialog.getWindow().getAttributes().width = LayoutParams.MATCH_PARENT;

        return dialog;
    }
}

这工作得很好,预计 TextView 颜色在前 Honeycomb 和 Honeycomb 设备上不正确。我使用的是 Holo.Light 主题,因此标准文本颜色为黑色,但在蜂巢之前的设备上对话框的背景颜色也是黑色。在 Honeycomb 设备上,对话框背景为白色。所以我所做的是,我在 values 文件夹的 styles.xml 中添加了 dialogTextColor=whitedialogTextColor=black< /code> 在 values-v11 文件夹中。然后我必须将样式属性添加到我在自定义对话框中使用的每个 TextView 。直到 ICS 才解决这个问题,原因很清楚 -> v11。我可以更改它,但我想要一个自定义对话框,它可以正确执行所有操作:预蜂窝、蜂窝、ICS(以及将来出现的任何内容)上的文本颜色基于应用程序主题、对话框的宽度、 AlertDialog 中的标准按钮、标题、图标。

What I'm trying to archive: I want a dialog with a custom view in it, but I want the standard icon, title, buttons from the AlertDialog.

What I'm doing is this custom dialog class:

public class CustomDialog extends AlertDialog.Builder {

    private Activity activity;
    private View root;

    public CustomDialog(Activity context) {
        super(context);
        this.activity = context;
    }

    public void setView(int layoutResID) {
        LayoutInflater inflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        root = inflater.inflate(layoutResID, (ViewGroup) activity.findViewById(R.id.dialog_root), false);
        ScrollView scroller = new ScrollView(activity);
        scroller.addView(root);
        setView(scroller);
    }

    public void setCustomView(View v) {
        ScrollView scroller = new ScrollView(activity);
        scroller.addView(v);
        setView(scroller);
    }

    public View getRoot() {
        return root;
    }

    @Override
    public AlertDialog create() {
        AlertDialog dialog = super.create();

        dialog.getWindow().getAttributes().width = LayoutParams.MATCH_PARENT;

        return dialog;
    }
}

This works pretty good, expect the TextView colors aren't correct on pre-Honeycomb and Honeycomb devices. I'm using the Holo.Light theme so the standard text color is black, but the background color of dialogs on pre-Honeycomb devices too. And on Honeycomb devices the dialog background is white. So what I did is, I added a dialogTextColor=white in the styles.xml in the values folder and dialogTextColor=black in the values-v11 folder. Then I had to add the style attribute to every TextView that I'm using in a custom dialog. This worked out until ICS and it's clear why -> v11. I could change it, but I want to have a custom dialog which does everything correct: The text color on pre-Honeycomb, Honeycomb, ICS (and whatever will come in the future) based on the application theme, the width of the dialog, the standard buttons, title, icon from the AlertDialog.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

场罚期间 2024-12-28 20:47:49

这里的技巧是上下文与主题相关联。该主题决定了各种事情,例如默认文本颜色等。

在蜂窝对话框之前,无论它们是从浅色主题活动还是深色主题活动生成的,总是具有相同的主题,并且除了列表之外,对话框都是深色背景,浅色前景。在 Honeycomb 及以后的版本中,对话框具有不同的主题,由生成它们的活动决定。

在对话框中扩充内容时,请始终使用 Dialog#getContext() 方法返回的上下文,而不是生成对话框的 Activity。不要使用上面用于获取 LayoutInflater 的代码行,而是尝试:

LayoutInflater inflater = LayoutInflater.from(getContext());

编辑:看起来您正在使用 AlertDialog.Builder 而不是 Dialog。 AlertDialog.Builder 为此在 API 11(Android 3.0,又名 Honeycomb)中添加了一个 getContext() 方法,但在此之前该方法并不存在。您可以使用适用于旧设备的 ContextThemeWrapper 构建您自己的主题上下文。只需确保您永远不会尝试在旧版本的平台上调用该方法。您可以通过简单的检查来保护它:

Context themedContext;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
    themedContext = getContext();
} else {
    themedContext = new ContextThemeWrapper(activity, android.R.style.Theme_Dialog);
}
LayoutInflater inflater = LayoutInflater.from(themedContext);

The trick here is that a Context is associated with a theme. That theme determines all sorts of things like default text colors, etc.

Before Honeycomb Dialogs always had the same theme whether they were generated from light or dark themed Activities, and with the exception of lists Dialogs were dark background, light foreground. In Honeycomb and forward, Dialogs have different themes determined by the Activity that spawns them.

When inflating content in a Dialog, always use the Context returned by the Dialog#getContext() method instead of the Activity that spawned the Dialog. Instead of the line of code you use to get your LayoutInflater above, try:

LayoutInflater inflater = LayoutInflater.from(getContext());

Edit: It looks like you're using an AlertDialog.Builder instead of a Dialog. AlertDialog.Builder added a getContext() method for this purpose in API 11 (Android 3.0, a.k.a. Honeycomb) but it didn't exist before that. You can build your own themed context with a ContextThemeWrapper for older devices. Just make sure you never try to call that method on an older version of the platform. You can guard it with a simple check:

Context themedContext;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
    themedContext = getContext();
} else {
    themedContext = new ContextThemeWrapper(activity, android.R.style.Theme_Dialog);
}
LayoutInflater inflater = LayoutInflater.from(themedContext);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文