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

[英]Changing the highlight and title color for fragments in PreferenceActivity


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).

我正在使用材料主题,我想改变下面图像中两个标记区域的颜色——即突出显示颜色(#1)和细节片段中标题的颜色(#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.

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

enter image description here

Any ideas?

什么好主意吗?

2 个解决方案

#1


22  

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

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

Item number 1

The attribute you'll want is activatedBackgroundIndicator.

您想要的属性是activatedBackgroundIndicator。

As per the docs

按照文档

Drawable used as a background for activated items.

可绘制的,用作激活项的背景。

Reason being

原因是

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:

PreferenceActivity中用于每个标题项的布局是preference_header_item_material,它使用激活的backgroundindicator作为背景。这个属性需要一个选择器,比如:

<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>

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.

这个项目稍微有点复杂。它不是你在另一个答案中建议的首选项,而是fragmentbread碎屑标题。不幸的是,不能使用主题设置标题颜色,因为用于样式化标题的属性是内部的私有属性。

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.

但是,您可以使用反射或仅仅通过遍历breadcrumb的视图层次结构来设置文本颜色,直到找到用于显示标题的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.

用于在每个PreferenceActivity中显示内容的布局是preference_list_content_material,它包含用于显示每个面包屑的breadcrumbs_in_fragment_material布局。从这个布局中可以看出,每个FragmentBreadCrums的id都是android:id/title。现在我们可以使用这个id查找面包屑并调整其中的TextView。

Using Reflection

使用反射

@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
    }

}

Traversing the view hierarchy

遍历视图层次

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

使用视图。findViewsWithText是一种简单的处理方法。

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

    });

Results

results

#2


0  

I have modified the suggested solution, now it works perfectly also for screen rotations.

我已经修改了建议的解决方案,现在它也可以很好地用于屏幕旋转。

In the base Activity class for our preferences we have to do the following:

在我们的首选项的基本活动类中,我们必须做以下工作:

@Override
public View onCreateView(String name, Context context, AttributeSet attrs)
{
    if ("android.app.FragmentBreadCrumbs".equals(name))
    {
        final View title = new android.app.FragmentBreadCrumbs(context, attrs);
        try {
            final Field titleColor = title.getClass().getDeclaredField("mTextColor");
            titleColor.setAccessible(true);
            titleColor.setInt(title, title_color));
        } catch (final Exception ignored) {
            // Nothing to do
        }
        return title;
    }
    return super.onCreateView(name, context, attrs);
}

and that's it.

就是这样。

智能推荐

注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:http://www.silva-art.net/blog/2014/11/14/84952d98d36d7e095fdd1999662c29a5.html



 
© 2014-2019 ITdaan.com 粤ICP备14056181号  

赞助商广告