首页 > 解决方案 > `android:breadCrumbShortTitle` 和 `android:breadCrumbTitle` 的区别

问题描述

在阅读android.R.attr文档时,我发现了breadCrumbTitleand breadCrumbShortTitle。这2个属性有什么用?android 是否在平台基础中提供了一个 breadCrumb 视图,如果有,它是什么样子的?为什么这两个属性存在?

标签: android

解决方案


它们用于PreferenceActivity

sa.peekValue(com.android.internal.R.styleable.PreferenceHeader_breadCrumbTitle);

具体来说,它们是在从XML 文件PreferenceActivity.Header中提取的实例上设置的:preference_headers

tv = sa.peekValue(com.android.internal.R.styleable.PreferenceHeader_breadCrumbTitle);
if (tv != null && tv.type == TypedValue.TYPE_STRING) {
    if (tv.resourceId != 0) {
        header.breadCrumbTitleRes = tv.resourceId;
    } else {
        header.breadCrumbTitle = tv.string;
    }
}

不幸的是,关于这个特性的作用的文档很少——它出现在哪里,它是如何在不同的 API 级别上使用的,等等。官方的设置指南甚至没有提到它们。

还有一个概念,FragmentBreadCrumbs但似乎没有使用此属性(并且记录得更少!)。

编辑:进一步看,事实证明这些功能是协同工作的!如果首选项标题设置了面包屑,则这些面包屑与小部件一起使用FragmentBreadCrumbs,假设存在一个带有 id的小部件android.R.id.title并且我们在一个多窗格首选项页面中:

/**
 * Change the base title of the bread crumbs for the current preferences.
 * This will normally be called for you.  See
 * {@link android.app.FragmentBreadCrumbs} for more information.
 */
public void showBreadCrumbs(CharSequence title, CharSequence shortTitle) {
    if (mFragmentBreadCrumbs == null) {
        View crumbs = findViewById(android.R.id.title);
        // For screens with a different kind of title, don't create breadcrumbs.
        try {
            mFragmentBreadCrumbs = (FragmentBreadCrumbs)crumbs;
        } catch (ClassCastException e) {
            setTitle(title);
            return;
        }
        if (mFragmentBreadCrumbs == null) {
            if (title != null) {
                setTitle(title);
            }
            return;
        }
        if (mSinglePane) {
            mFragmentBreadCrumbs.setVisibility(View.GONE);
            // Hide the breadcrumb section completely for single-pane
            View bcSection = findViewById(com.android.internal.R.id.breadcrumb_section);
            if (bcSection != null) bcSection.setVisibility(View.GONE);
            setTitle(title);
        }
        mFragmentBreadCrumbs.setMaxVisible(2);
        mFragmentBreadCrumbs.setActivity(this);
    }
    if (mFragmentBreadCrumbs.getVisibility() != View.VISIBLE) {
        setTitle(title);
    } else {
        mFragmentBreadCrumbs.setTitle(title, shortTitle);
        mFragmentBreadCrumbs.setParentTitle(null, null, null);
    }
}

推荐阅读