如何使用支持操作栏将默认主页显示为向上指示符?

时间:2015-05-15 14:55:05

标签: android android-actionbar android-support-library android-toolbar

在我的应用程序中,我将home作为向上指示符设置为某些情况下的非默认drawable。有时,作为向上指示符的主页应在更改后重置为默认图标。

我尝试过使用

getSupportActionBar().setHomeAsUpIndicator(0);

根据documentation所说的将使用主题默认值。当我这样做时,应用程序崩溃并出现以下错误:

05-15 11:12:03.160: E/AndroidRuntime(13021): FATAL EXCEPTION: main
05-15 11:12:03.160: E/AndroidRuntime(13021): Process: xxxxx.yyyyyy.zzzzzz, PID: 13021
05-15 11:12:03.160: E/AndroidRuntime(13021): android.content.res.Resources$NotFoundException: Resource ID #0x0
05-15 11:12:03.160: E/AndroidRuntime(13021):    at android.content.res.Resources.getValue(Resources.java:1233)
05-15 11:12:03.160: E/AndroidRuntime(13021):    at android.content.res.Resources.getDrawable(Resources.java:756)
05-15 11:12:03.160: E/AndroidRuntime(13021):    at android.content.Context.getDrawable(Context.java:402)
05-15 11:12:03.160: E/AndroidRuntime(13021):    at android.support.v4.content.ContextCompatApi21.getDrawable(ContextCompatApi21.java:26)
05-15 11:12:03.160: E/AndroidRuntime(13021):    at android.support.v4.content.ContextCompat.getDrawable(ContextCompat.java:319)
05-15 11:12:03.160: E/AndroidRuntime(13021):    at android.support.v7.internal.widget.TintManager.getDrawable(TintManager.java:133)
05-15 11:12:03.160: E/AndroidRuntime(13021):    at android.support.v7.widget.Toolbar.setNavigationIcon(Toolbar.java:754)
05-15 11:12:03.160: E/AndroidRuntime(13021):    at android.support.v7.internal.app.ToolbarActionBar.setHomeAsUpIndicator(ToolbarActionBar.java:176)
05-15 11:12:03.160: E/AndroidRuntime(13021):    at xxxxx.yyyyyy.zzzzzz.updateHomeIndicator(aaaaa.java:1711)

我知道主题默认值设置正确,因为如果我从未将自定义主页设置为向上指示符,则后退箭头显示正常。我在尝试setDisplayHomeAsUpEnabled(true)来电之前打电话给setHomeAsUpIndicator(0)

我知道我总是可以将默认主页作为向上指标资产复制到我的项目中并直接使用该资产,但我想尽可能避免这种解决方法。

谢谢!

扎克

编辑:

我也试过

getSupportActionBar().setHomeAsUpIndicator(null);

这导致无法显示任何图标。

1 个答案:

答案 0 :(得分:0)

我遵循以下源代码:node.js home page

当我看到源代码:ToolbarActionBar.setHomeAsUpIndicator()时,它说:“值0是无效的标识符”。

 /**
 * Return a drawable object associated with a particular resource ID.
 * <p>
 * Starting in {@link android.os.Build.VERSION_CODES#LOLLIPOP}, the returned
 * drawable will be styled for the specified Context's theme.
 *
 * @param id The desired resource identifier, as generated by the aapt tool.
 *            This integer encodes the package, type, and resource entry.
 *            The value 0 is an invalid identifier.
 * @return Drawable An object that can be used to draw this resource.
 */
public static final Drawable getDrawable(Context context, int id) {
    final int version = Build.VERSION.SDK_INT;
    if (version >= 21) {
        return ContextCompatApi21.getDrawable(context, id);
    } else {
        return context.getResources().getDrawable(id);
    }
}

我一直关注源代码,最后看到下面的代码,这是你抛出异常的地方:ContextCompat.getDrawable()

/**
 * Return the raw data associated with a particular resource ID.
 * 
 * @param id The desired resource identifier, as generated by the aapt
 *           tool. This integer encodes the package, type, and resource
 *           entry. The value 0 is an invalid identifier.
 * @param outValue Object in which to place the resource data.
 * @param resolveRefs If true, a resource that is a reference to another
 *                    resource will be followed so that you receive the
 *                    actual final resource data.  If false, the TypedValue
 *                    will be filled in with the reference itself.
 *
 * @throws NotFoundException Throws NotFoundException if the given ID does not exist.
 *
 */
public void getValue(int id, TypedValue outValue, boolean resolveRefs)
        throws NotFoundException {
    boolean found = mAssets.getResourceValue(id, 0, outValue, resolveRefs);
    if (found) {
        return;
    }
    throw new NotFoundException("Resource ID #0x"
                                + Integer.toHexString(id));
}

到目前为止,我看不到对'0'的任何特殊处理。所以,我认为这是你在上面发布的官方文档中的一个错误。解决此崩溃错误的简便方法是使用现有标识符,就像您的默认图标ID一样。