且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

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

更新时间:2022-06-20 08:28:56

我知道每个地方的颜色都有一个属性 主题,但我似乎找不到.

I know there's an attribute for the color of each somewhere in the theme, but I just can't seem to find it.

项目编号1

您需要的属性是 activatedBackgroundIndicator

根据文档

Drawable用作激活项的背景.

Drawable used as a background for activated items.

原因

用于PreferenceActivity中每个标题项的布局为

The layout used for each header item in a PreferenceActivity is preference_header_item_material which uses the activatedBackgroundIndicator as a background. You'll need a selector for this attribute, something like:

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

    <item android:state_activated="true"><color android:color="yourColor" />
    </item>
    <item><color android:color="@android:color/transparent" />
    </item>

</selector>

商品编号2

这个项目有点棘手.它不是您在其他答案中建议的PreferenceScreen,而是 FragmentBreadCrumbs 标题.不幸的是,无法使用主题设置标题颜色,因为用于为其设置样式的属性是内部私有的.

Item number 2

This item is slightly trickier. It's not a PreferenceScreen like you suggested in the other answer, but the FragmentBreadCrumbs title. Unfortunately, the title color cannot be set using a theme because the attribute used to style it is private internal.

但是,您可以使用反射"或仅遍历面包屑的视图层次结构,直到找到用于显示标题的TextView来设置文本颜色.

You can however set the text color using Reflection or just by traversing the breadcrumb's view hierarchy until you find the TextView used to display the title.

用于在每个PreferenceActivity中显示内容的布局为 breadcrumbs_in_fragment_material 布局以显示每个面包屑.您可以从该布局中看到每个FragmentBreadCrums的ID为android:id/title.现在,我们可以使用此ID查找面包屑并调整其中的TextView.

The layout used to display the content in each PreferenceActivity is preference_list_content_material which includes the breadcrumbs_in_fragment_material layout to display each breadcrumb. You can see from that layout that the id of each FragmentBreadCrums is android:id/title. Now we can use this id to find the breadcrumb and adjust the the TextView inside it.

使用反射

@Override
public void onBuildHeaders(List<Header> target) {
    super.onBuildHeaders(target);
    loadHeadersFromResource(R.xml.yourPreferenceHeaders, target);

    final View breadcrumb = findViewById(android.R.id.title);
    if (breadcrumb == null) {
        // Single pane layout
        return;
    }

    try {
        final Field titleColor = breadcrumb.getClass().getDeclaredField("mTextColor");
        titleColor.setAccessible(true);
        titleColor.setInt(breadcrumb, yourTitleColor);
    } catch (final Exception ignored) {
        // Nothing to do
    }

}

遍历视图层次结构

使用

Using View.findViewsWithText is an easy way to handle this.

    breadcrumb.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener() {

        @Override
        public void onGlobalLayout() {
            breadcrumb.getViewTreeObserver().removeOnGlobalLayoutListener(this);

            final ArrayList<View> outViews = Lists.newArrayList();
            breadcrumb.findViewsWithText(outViews, getString(R.string.your_header_title),
                    View.FIND_VIEWS_WITH_TEXT);

            ((TextView) outViews.get(0)).setTextColor(yourTitleColor);
        }

    });

结果