工具栏导航图标从未被设置

72

我正在尝试使用新的工具栏组件,并且在导航图标方面遇到了一些问题。我想实现自定义的返回导航图标:

在我的清单文件中,我将父级设置为我的活动:

<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);

这给了我:

带有返回按钮的工具栏

返回图标不是setNavigationIcon()方法设置的那个!无论我给该方法提供什么可绘制对象,导航图标始终为返回箭头。

我尝试在清单中删除父级关联,但唯一的效果是(显然)防止按钮返回。

相反,如果我想要默认的返回箭头图标并且不调用setNavigationIcon(),我就没有任何图标。

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

注意:我正在Android 4.4上运行测试


我已经做了很多次,但现在不知道为什么不起作用了? - filthy_wizard
值得注意的是,不建议添加app:navigationIcon,因为“所有Android设备都提供了一个返回按钮来进行此类导航,因此您不应该在应用程序的用户界面中添加返回按钮”。https://developer.android.com/guide/navigation/navigation-custom-back - apex39
13个回答

1

为我工作...

<android.support.v7.widget.Toolbar
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/toolBar"
        android:background="@color/colorGreen"
        app:title="Title"
        app:titleTextColor="@color/colorBlack"
        app:navigationIcon="@drawable/ic_action_back"/>

1

如果您不希望将工具栏设置为操作栏,可以使用以下内容:

        val toggle = ActionBarDrawerToggle(this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close)
        toggle.isDrawerSlideAnimationEnabled = false
        toggle.isDrawerIndicatorEnabled = false
        toggle.setHomeAsUpIndicator(AppCompatResources.getDrawable(this, ...))
        drawer!!.addDrawerListener(toggle)
        toggle.setToolbarNavigationClickListener {
            setDrawerOpened(!isDrawerOpened())
        }
        toggle.syncState()

fun setDrawerOpened(open: Boolean) {
    if (open == drawerLayout.isDrawerOpen(GravityCompat.START))
        return
    if (open)
        drawerLayout.openDrawer(GravityCompat.START)
    else drawerLayout.closeDrawer(GravityCompat.START)
}

在尝试了许多不同的选项后,这个解决方案对我起作用了。我相信这个选项toggle.isDrawerIndicatorEnabled = false是确保可以设置新图标的关键。 - Anil Gorthy

-1

试试这个:

  <android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:toolbar="http://schemas.android.com/apk/res-auto"
        android:id="@+id/tool_drawer"
        android:layout_width="match_parent"
        android:layout_height="?actionBarSize"
        toolbar:navigationIcon="@drawable/ic_navigation">

    </android.support.v7.widget.Toolbar>

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接