我正在尝试使用新的工具栏组件,并且在导航图标方面遇到了一些问题。我想实现自定义的返回导航图标:
在我的清单文件中,我将父级设置为我的活动:
<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>
然后在我的活动中,我像这样配置工具栏:
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);
这给了我:
![带有返回按钮的工具栏](https://istack.dev59.com/Oul74.webp)
返回图标不是setNavigationIcon()
方法设置的那个!无论我给该方法提供什么可绘制对象,导航图标始终为返回箭头。
我尝试在清单中删除父级关联,但唯一的效果是(显然)防止按钮返回。
相反,如果我想要默认的返回箭头图标并且不调用setNavigationIcon()
,我就没有任何图标。
处理工具栏中导航图标(自定义和默认)的正确方式是什么?
注意:我正在Android 4.4上运行测试