工具栏导航图标从未设置 [英] Toolbar navigation icon never set

查看:236
本文介绍了工具栏导航图标从未设置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想新的工具栏组成部分,具有导航图标一些麻烦。 我想要实现的后退导航自定义图标:

I'm trying the new Toolbar component and having some trouble with the navigation icon. I want to implement a custom icon for back navigation :

在我的清单我设置了父母给我的活动:

In my manifest i set a parent to my activity :

<activity android:name=".CardsActivity" android:parentActivityName=".MainActivity">
    <!-- Parent activity meta-data to support API level 7+ -->
    <meta-data
        android:name="android.support.PARENT_ACTIVITY"
        android:value=".MainActivity" />
</activity>

我宣布这样的工具栏:

I declare the toolbar like this :

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    tools:context="com.example.lollitest.MainActivity" >

    <android.support.v7.widget.Toolbar
        android:id="@+id/my_awesome_toolbar"
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        android:minHeight="?attr/actionBarSize"
        android:layout_marginBottom="10dp"
        android:background="?attr/colorPrimary" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/my_awesome_toolbar"
        android:text="@string/hello_world" />

</RelativeLayout>

然后在我的活动我配置的工具栏是这样的:

Then in my activity i configure the Toolbar like this :

Toolbar toolbar = (Toolbar) findViewById(R.id.my_awesome_toolbar);
toolbar.setNavigationIcon(R.drawable.ic_good);
toolbar.setTitle("Title");
toolbar.setSubtitle("Sub");
toolbar.setLogo(R.drawable.ic_launcher);
setSupportActionBar(toolbar);

这给我:

Which giving me :

返回图标的不是一个我设置 setNavigationIcon()!无论绘制我给该方法的导航图标总是回头箭。

The back icon is not the one i set with setNavigationIcon() ! Whatever drawable i give to the method the navigation icon is always the back arrow.

我试图删除家长会在清单中,但唯一的影响是(显然),以prevent的按钮返回。

I have tried to remove the parent association in the manifest but the only effect is (obviously) to prevent the button to go back.

在相反,如果我想的默认后退箭头图标,不叫 setNavigationIcon()我没有任何图标都没有。

On contrary if i want the default back arrow icon and don't call setNavigationIcon() i don't have any icon at all.

什么是正确的方式来处理在工具栏上的导航图标(自定义和默认)?

注:我在Android 4.4上运行我的测试

NOte : i'm running my test on Android 4.4

推荐答案

目前,你可以用它,改变了顺序:(这似乎是一个错误)

Currently you can use it, changing the order: (it seems to be a bug)

Toolbar toolbar = (Toolbar) findViewById(R.id.my_awesome_toolbar);
setSupportActionBar(toolbar);

toolbar.setNavigationIcon(R.drawable.ic_good);
toolbar.setTitle("Title");
toolbar.setSubtitle("Sub");
toolbar.setLogo(R.drawable.ic_launcher);

这篇关于工具栏导航图标从未设置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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