【问题标题】:Changing the highlighting colors (button colors) of a "DialogPreference"更改“DialogPreference”的突出显示颜色(按钮颜色)
【发布时间】:2014-03-11 15:00:42
【问题描述】:

我完全按照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

【问题讨论】:

  • 您的布局是自定义或默认的
  • 看看最新的更新:)

标签: android styles themes


【解决方案1】:

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

截图

【讨论】:

  • 感谢您的回答,但恐怕这不是我要找的答案。我想在按下按钮的那一刻改变它的颜色:)
  • 是的,没错。我的示例不包括selector,但这就是您所需要的。然后只需致电View.setBackgroundDrawable。我会进行编辑。
  • 是的。没想到:)谢谢
【解决方案2】:

如果您无法找到根据自己的喜好设置内置按钮样式的解决方案,您实际上可以在自定义布局的底部添加一个按钮行,其外观和行为与内置按钮完全相同。然后将按钮侦听器设置为自定义按钮栏的按钮,这将导致没有内置按钮栏。

通过这种方式,您可以让它们看起来随心所欲!

【讨论】:

    【解决方案3】:

    你可以试试this Answer。在这里你不需要编写代码只需要自定义 AlertDialog 主题。

    自定义主题后,它可能适用于您的完整应用程序。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-12-12
      • 2012-10-24
      • 1970-01-01
      • 1970-01-01
      • 2013-02-02
      • 2023-03-19
      相关资源
      最近更新 更多