从未设置过的工具栏导航图标 [英] Toolbar navigation icon never set
问题描述
我正在试用新的工具栏组件,但在导航图标方面遇到了一些问题.我想为后退导航实现自定义图标:
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>
我像这样声明工具栏:
<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);
这给了我:
后退图标不是我用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.
我试图删除清单中的父关联,但唯一的效果是(显然)防止按钮返回.
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
推荐答案
目前可以使用了,换个顺序:(好像是bug)
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屋!