简体   繁体   中英

Android Activity Toolbar Up Button not showing

I have created an activity to hold fragments. There is no up button displayed in the toolbar when it is run or in the xml preview.

This is the code I used to add the toolbar to the activity

Toolbar mToolbar = (Toolbar) findViewById(R.id.category_toolbar);
setSupportActionBar(mToolbar);

if(getSupportActionBar() != null){
    getSupportActionBar().setTitle("Test");
    getSupportActionBar().setDefaultDisplayHomeAsUpEnabled(true);
    getSupportActionBar().setDisplayShowHomeEnabled(true);
}

I've used all the correct libraries (AppCompatActivity). This Activity holds a fragment and thats were it doesn't show.

XML for the activity

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:orientation="vertical"
              android:layout_width="match_parent"
              android:layout_height="match_parent">

    <android.support.v7.widget.Toolbar
        android:id="@+id/category_toolbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        style="@style/toolbar">
    </android.support.v7.widget.Toolbar>

    <LinearLayout
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/fragment_container">

    </LinearLayout>

</LinearLayout>

I've also set the parent activity in the manifest file

 <activity android:name=".TestActivity"
                  android:parentActivityName=".MainActivity">

        </activity>

Is there anything else I need to add to make it work?

Thanks

Since you are using AppCompatActivity as base activity, using setDisplayHomeAsUpEnabled(true) rather than setDefaultDisplayHomeAsUpEnabled(true) , would resolve your problem.

setDefaultDisplayHomeAsUpEnabled(true) would work if you use ActionBarActivity.

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