当前位置: 动力学知识库 > 问答 > 编程问答 >

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 doesnt show up unless the android:parentActivityName is specified.

Instead, try to use:

getSupportActionBar().setDisplayHomeAsUpEnabled(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.

分享给朋友:
您可能感兴趣的文章:
随机阅读: