在PreferenceActivity中更改片段的突出显示和标题颜色 [英] Changing the highlight and title color for fragments in PreferenceActivity

查看:70
本文介绍了在PreferenceActivity中更改片段的突出显示和标题颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是Material主题,我想更改下图中两个标记区域的颜色-即突出显示颜色(#1)和细节片段中标题的颜色(# 2).

I'm using the Material theme, and I'd like to change the color of the two marked areas in the image below - i.e. the highlight color (#1) and the color of the title in the details fragment (#2).

我知道主题中每个位置的颜色都有一个属性,但我似乎找不到它.

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

有什么想法吗?

推荐答案

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

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中显示内容的布局为

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

    });

结果

这篇关于在PreferenceActivity中更改片段的突出显示和标题颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆