Android活动工具栏向上按钮未显示

Android活动工具栏向上按钮未显示,android,android-layout,android-toolbar,Android,Android Layout,Android Toolbar,我创建了一个活动来保存片段。运行工具栏或xml预览时,工具栏中没有显示“向上”按钮 这是我用来将工具栏添加到活动中的代码 Toolbar mToolbar = (Toolbar) findViewById(R.id.category_toolbar); setSupportActionBar(mToolbar); if(getSupportActionBar() != null){ getSupportActionBar().setTitle("Test"); getSuppo

我创建了一个活动来保存片段。运行工具栏或xml预览时,工具栏中没有显示“向上”按钮

这是我用来将工具栏添加到活动中的代码

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

if(getSupportActionBar() != null){
    getSupportActionBar().setTitle("Test");
    getSupportActionBar().setDefaultDisplayHomeAsUpEnabled(true);
    getSupportActionBar().setDisplayShowHomeEnabled(true);
}
<?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>
我使用了所有正确的库(AppCompatActivity)。此活动包含一个片段,如果没有显示,则为

活动的XML

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

if(getSupportActionBar() != null){
    getSupportActionBar().setTitle("Test");
    getSupportActionBar().setDefaultDisplayHomeAsUpEnabled(true);
    getSupportActionBar().setDisplayShowHomeEnabled(true);
}
<?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>

我还在清单文件中设置了父活动

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

        </activity>

我还有什么需要补充的吗


谢谢

因为您正在使用
AppCompatActivity
作为基本活动,所以使用
setDisplayHomeAsUpEnabled(true)
而不是
setDefaultDisplayHomeAsUpEnabled(true)
将解决您的问题


setDefaultDisplayHomeAsUpEnabled(true)
如果您使用
ActionBarActivity将起作用。

您通常对片段容器使用框架布局,但这似乎没有什么问题<代码>getSupportActionBar().setDisplayHomeAsUpEnabled(true)应该就是你所需要的了谢谢@cricket_007,我用错了方法哈哈