简体   繁体   中英

Android Home button on Action Bar Does Not Appear

I came across this strange problem where the Home button on toolbar is not showing.

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

I have another app using the same method, which is working fine. Any idea?

You already have :

<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"/>

Which it has a default icon:

在此处输入图片说明

And take a look at this blog: codetheory.in (web.archive)

Which says:

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.

Instead, try to use:

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

Then, it should work without that Drawer i guess .because it has a default icon.

Check the results without Drawer with my suggested code: 在此处输入图片说明

Somehow, you don't need that.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM