Android主题首选项对话框

时间:2014-03-23 00:19:48

标签: android themes

我有一个应用程序,它使用首选项活动来设置一些用户设置。我一整天都想弄清楚这一点。我试图在用户按下编辑文本首选项对象时设置警告对话框的主题。将打开一个对话框,用户可以设置共享首选项。弹出对话框:

enter image description here

我希望文字为绿色。我希望分频器绿色。线条和光标绿色。

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

<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>

有人可以指出我正确的方向或可能分享一些代码。我迷路了。我一直在网上冲浪,以寻找一天中的大部分时间。提前谢谢。

2 个答案:

答案 0 :(得分:5)

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

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));
        }
    }
}

实施

<EditTextPreference.../>替换为xml中的<path_to_CustomDialogPreference.../>

注意

我使用Android Holo ColorsEditText创建了背景。

Example

答案 1 :(得分:0)

您可以使用自己的自定义组件为自己的对话框主题构建自定义布局,也可以使用外部库,例如android-styled-dialogs

所以在这种情况下,使用可以根据需要自定义对话框:

<style name="DialogStyleLight.Custom">
    <!-- anything can be left out: -->
    <item name="titleTextColor">@color/dialog_title_text</item>
    <item name="titleSeparatorColor">@color/dialog_title_separator</item>
    <item name="messageTextColor">@color/dialog_message_text</item>
    <item name="buttonTextColor">@color/dialog_button_text</item>
    <item name="buttonSeparatorColor">@color/dialog_button_separator</item>
    <item name="buttonBackgroundColorNormal">@color/dialog_button_normal</item>
    <item name="buttonBackgroundColorPressed">@color/dialog_button_pressed</item>
    <item name="buttonBackgroundColorFocused">@color/dialog_button_focused</item>
    <item name="dialogBackground">@drawable/dialog_background</item>
</style>