简体   繁体   English

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

[英]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. 运行工具栏或xml预览时,工具栏中没有显示向上按钮。

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). 我已经使用了所有正确的库(AppCompatActivity)。 This Activity holds a fragment and thats were it doesn't show. 此活动包含一个片段,多数民众赞成在没有显示的情况下。

XML for the activity 活动的XML

<?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. 由于您将AppCompatActivity用作基本活动,因此使用setDisplayHomeAsUpEnabled(true)而不是setDefaultDisplayHomeAsUpEnabled(true)可以解决您的问题。

setDefaultDisplayHomeAsUpEnabled(true) would work if you use ActionBarActivity. 如果使用ActionBarActivity.setDefaultDisplayHomeAsUpEnabled(true)将起作用ActionBarActivity.

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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