更改“ DialogPreference”的突出显示颜色(按钮颜色)

芦香

完全按照http://www.lukehorvat.com/blog/android-seekbardialogpreference中说明的方式实现了DialogPreference

另外,我能够更改DialogPreference的文本和分隔符颜色,但是当按钮被按下时,我无法更改其突出显示颜色。有人知道怎么做这个吗?

更新:

我将以下布局用于DialogPreference

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:orientation="vertical">
<TextView
        android:id="@+id/text_dialog_message"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="6dip"
        android:paddingLeft="12dip"
        android:paddingRight="12dip"/>
<TextView
        android:id="@+id/text_progress"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="6dip"
        android:gravity="center_horizontal"/>
<SeekBar
        android:id="@+id/seek_bar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="6dip"
        android:layout_marginTop="6dip"/>
</LinearLayout>

到目前为止,与该DialogPreference或我更改的布局有关的唯一样式属性是通过编程方式更改的:

        int alertTitleId = this.getContext().getResources().getIdentifier("alertTitle", "id", "android");
        TextView alertTitle = (TextView) getDialog().getWindow().getDecorView().findViewById(alertTitleId);
        alertTitle.setTextColor(color); // change title text color

        int titleDividerId = this.getContext().getResources().getIdentifier("titleDivider", "id", "android");
        View titleDivider = getDialog().getWindow().getDecorView().findViewById(titleDividerId);
        titleDivider.setBackgroundColor(color); // change divider color
肾上腺的

您需要做的只是subclass DialogPreference,然后调用Resource.getIdentifier以查找View想要主题的每个对象,就像您在做的一样,但是您无需调用Window.getDecorView这是一个例子:

风俗 DialogPreference

public class CustomDialogPreference extends DialogPreference {

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

        // Button views
        window.findViewById(res.getIdentifier("button1", "id", "android"))
                .setBackgroundDrawable(res.getDrawable(R.drawable.your_selector));
        window.findViewById(res.getIdentifier("button2", "id", "android"))
                .setBackgroundDrawable(res.getDrawable(R.drawable.your_selector));
        window.findViewById(res.getIdentifier("button3", "id", "android"))
                .setBackgroundDrawable(res.getDrawable(R.drawable.your_selector));

    }

}

XML首选项

<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" >

    <path_to.CustomDialogPreference
        android:dialogMessage="Message"
        android:negativeButtonText="Cancel"
        android:positiveButtonText="Okay"
        android:title="Title" />

</PreferenceScreen>

自定义选择器

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" android:autoMirrored="true">

    <item android:drawable="@drawable/your_pressed_drawable" android:state_pressed="true"/>
    <item android:drawable="@drawable/your_default_drawable"/>

</selector>

备用选择器

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" android:autoMirrored="true">

    <item android:drawable="@color/your_pressed_color" android:state_pressed="true"/>
    <item android:drawable="@color/your_default_color/>

</selector>

截屏

例子

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

更改Roku屏幕模板中按钮的突出显示颜色

突出显示“全选”的颜色更改

突出显示文字并更改颜色

如何更改UIButton的突出显示颜色?

在Eclipse中更改字段的突出显示颜色

如何更改文本突出显示颜色?

更改浮点中的突出显示点颜色

如何更改标签栏突出显示的颜色

如何更改UITableViewCell的蓝色突出显示颜色?

更改突出显示的UIButton边框颜色

如何更改Netbeans的突出显示颜色?

更改表格选择多个突出显示颜色

高位更改悬停时突出显示颜色

更改“鼠标突出显示”颜色的代码

突出显示元素时更改链接的颜色

无法更改UITableView蓝色突出显示颜色

如何更改 VSCode 搜索突出显示颜色

更改多个按钮的突出显示颜色后,单击时按钮会消失

浮动动作按钮:如何像在RaisedButton中一样更改初始颜色和突出显示颜色?

如何在Flutter中更改大纲按钮的边框突出显示颜色?

如何在 NativeScript 中为 iOS 更改突出显示状态下的按钮文本颜色?

Eclipse:如何更改“查找/替换”搜索突出显示颜色?

如何更改NSTableCellView中NSTextField的突出显示颜色?

如何在Eclipse中更改Checkstyle警告的突出显示颜色?

更改角材料输入文本框的突出显示颜色

在vs代码中更改错误突出显示颜色

在Flutter中更改文本字段的突出显示颜色

在PreferenceActivity中更改片段的突出显示和标题颜色

在Visual Studio Code中更改突出显示的文本颜色