Changing the highlight and title color for fragments in PreferenceActivity

倖福魔咒の 提交于 2019-12-08 16:18:14

问题


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.

Any ideas?


回答1:


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.

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:

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

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.

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.

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.

    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




回答2:


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.



来源:https://stackoverflow.com/questions/26922915/changing-the-highlight-and-title-color-for-fragments-in-preferenceactivity

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!