gpt4 book ai didi

android - 更改 "DialogPreference"的突出显示颜色(按钮颜色)

转载 作者:太空宇宙 更新时间:2023-11-03 13:24:37 25 4
gpt4 key购买 nike

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

截图

Example

关于android - 更改 "DialogPreference"的突出显示颜色(按钮颜色),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22329530/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com