Android 操作栏上的主页按钮未出现

Android Home button on Action Bar Does Not Appear

我遇到了一个奇怪的问题,工具栏上的主页按钮没有显示。

activity_main.xml

    <?xml version="1.0" encoding="utf-8"?>

<android.support.v4.widget.DrawerLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:elevation="@dimen/design_navigation_elevation"
    android:fitsSystemWindows="true"
    tools:context="my.app.MainActivity">

    <android.support.design.widget.CoordinatorLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <include layout="@layout/toolbar"/>
        <include layout="@layout/content_main"/>
        <include layout="@layout/fab"/>

    </android.support.design.widget.CoordinatorLayout>

    <android.support.design.widget.NavigationView
        android:id="@+id/navigation_drawer"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        app:headerLayout="@layout/navigation_drawer_header"
        app:menu="@menu/menu_navigation_drawer"/>

</android.support.v4.widget.DrawerLayout>

toolbar.xml

    <?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.AppBarLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_height="wrap_content"
    android:layout_width="match_parent"
    android:theme="@style/AppTheme.AppBarOverlay">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="?attr/colorPrimary"
        app:popupTheme="@style/AppTheme.PopupOverlay" />

</android.support.design.widget.AppBarLayout>

MainActivity.java

public class MainActivity extends AppCompatActivity {

private Toolbar toolbar;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

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

    setSupportActionBar(toolbar);
    getSupportActionBar().setDisplayShowHomeEnabled(true);

我有另一个使用相同方法的应用程序,它工作正常。有什么想法吗?

您已经拥有:

<android.support.design.widget.NavigationView
        android:id="@+id/navigation_drawer"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        app:headerLayout="@layout/navigation_drawer_header"
        app:menu="@menu/menu_navigation_drawer"/>

它有一个默认图标:

看看这个博客:codetheory.in (web.archive)

这表示:

setHomeButtonEnabled(boolean enabled)

This method is similar to the previous one(setDisplayHomeAsUpEnabled(boolean showHomeAsUp)) actually, except that the left-facing caret doesn’t show up unless the android:parentActivityName is specified.

改为尝试使用:

getSupportActionBar().setDisplayHomeAsUpEnabl‌​‌​ed(true);

然后,它应该没有那个 Drawer 我猜。因为它有一个默认图标。

使用我建议的代码检查没有 Drawer 的结果:

不知何故,你不需要那个。