Android主题首选项对话框 [英] Android Theming Preference Dialog

查看:74
本文介绍了Android主题首选项对话框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用首选项活动设置某些用户设置的应用程序.我一直试图解决这一问题.我正在尝试在用户按下编辑文本首选项"对象时设置警报对话框的主题.将打开一个对话框,用户可以设置共享首选项.对话框弹出:

I have an application that uses a preference activity to set some user settings. I been trying to figure this out all day. I am trying to theme the alert dialog when an user presses an Edit Text Preference object. A dialog opens up and the user can set the shared preference. The dialog pops up:

我希望文本为绿色.我想要分隔线为绿色.该行和光标为绿色.

I want the text green. I want the divider green. The line and cursor green.

这是我到目前为止所拥有的.

This is what I have so far.

<style name="CustomDialogTheme" parent="@android:style/Theme.Dialog">
    <item name="android:background">@color/text_green</item>
    <item name="android:textColor">@color/text_green</item>
</style>

有人可以向我指出正确的方向,还是可以分享一些代码.我迷路了.我一天中大部分时间都在网上冲浪.预先感谢.

Can someone point me in the right direction or maybe share some code. I am at lost. I've been surfing the net to find something most of the day. Thanks in advance.

推荐答案

如果您不想创建自定义布局或使用第三方库,则可以将 EditTextPreference 子类化,然后访问每个您要通过使用 Resources.getIdentifier 然后使用 Window.findViewById 进行编辑的 View .这是一个简单的例子.

If you don't want to create a custom layout or use a third party library, you can subclass EditTextPreference, then access each View you want to edit by using Resources.getIdentifier then using Window.findViewById. Here's a quick example.

public class CustomDialogPreference extends EditTextPreference {

    public CustomDialogPreference(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    public CustomDialogPreference(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    protected void showDialog(Bundle state) {
        super.showDialog(state);
        final Resources res = getContext().getResources();
        final Window window = getDialog().getWindow();
        final int green = res.getColor(android.R.color.holo_green_dark);

        // Title
        final int titleId = res.getIdentifier("alertTitle", "id", "android");
        final View title = window.findViewById(titleId);
        if (title != null) {
            ((TextView) title).setTextColor(green);
        }

        // Title divider
        final int titleDividerId = res.getIdentifier("titleDivider", "id", "android");
        final View titleDivider = window.findViewById(titleDividerId);
        if (titleDivider != null) {
            titleDivider.setBackgroundColor(green);
        }

        // EditText
        final View editText = window.findViewById(android.R.id.edit);
        if (editText != null) {
            editText.setBackground(res.getDrawable(R.drawable.apptheme_edit_text_holo_light));
        }
    }
}

实施

在您的xml中用< path_to_CustomDialogPreference .../> 替换< EditTextPreference .../> .

Replace <EditTextPreference.../> with <path_to_CustomDialogPreference.../> in your xml.

注意

我使用 Android Holo颜色 EditText 创建背景

这篇关于Android主题首选项对话框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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