Android 主题首选项对话框

标签 android themes

我有一个应用程序使用首选项 Activity 来设置一些用户设置。我整天都在想办法解决这个问题。当用户按下“编辑文本首选项”对象时,我正在尝试设置警报对话框的主题。将打开一个对话框,用户可以设置共享首选项。弹出对话框:

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>

有人能给我指出正确的方向或者分享一些代码吗?我迷路了。我一天中的大部分时间都在网上冲浪以寻找一些东西。提前致谢。

最佳答案

如果您不想创建自定义布局或使用第三方库,您可以子类化 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.../><path_to_CustomDialogPreference.../>在你的 xml 中。

注意

我用了Android Holo ColorsEditText 创建背景.

Example

关于Android 主题首选项对话框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22585448/

相关文章:

android - 以编程方式应用 css、样式或主题 android

android - 获取带有全名详细信息和电话号码的单个光标

android - Google Play 一直将 "Text"这个词注入(inject)我的数据库,我无法阻止它

java - 我的回收器 View 中的对象列表最大为 20

android - 有什么方法可以将我的移动存储中的文件夹与我的 AWS S3 存储桶中的文件夹同步吗?

css - 下拉 Wordpress Nav 在 Firefox 上不起作用

android - 可以更改主题的某些方面(颜色)

android - 每次放大或缩小图像时检测

html - 编辑 HTML/CSS 主题,但似乎无法正确编辑

themes - 如何获得完全黑暗的基巴纳?