android-更改“ 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

解决方法:

您需要做的只是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>

屏幕截图

上一篇:c# – 重写的TextBlock样式的奇怪行为


下一篇:深入react技术栈记录(一)