简体   繁体   中英

Android - ActionBar Up button not visible when using TextView as ToolBar title

I have added a Toolbar on top of all of my activities. But I wanted the title to be in the centre, so as per some suggestions on SO, I created a TextView inside the Toolbar and I was able to put it in the centre.
Now, when I am trying to add an Up Action following this guide Adding an Up Action | Android Developers , I do not get any UpButton.
Is this because of using a TextView inside of the Toolbar ? Because I read on SO that the Toolbar is basically a View and can be configured just like any other View , so I don't get why this should be a problem.

  1. If that is the reason, and I'd have to use the default android:title="My Title" attribute in for the Toolbar` , is there any other way to put it in the centre ?
  2. What is the workaround for this problem so that I can get an Up Action as well as the toolbar title in the centre ?

Thanks for any help you'd be able to provide.

XML Layout

<LinearLayout
    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:layout_height="match_parent"
    android:orientation="vertical"
    android:layout_width="match_parent">
    <android.support.v7.widget.Toolbar
        android:id="@+id/lifeline_toolbar"
        android:elevation="4dp"
        android:background="@android:color/background_dark"
        android:theme="@style/ThemeOverlay.AppCompat.ActionBar"
        app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize">
        <TextView
            android:layout_gravity="center_horizontal"
            android:text="Lifeline"
            android:textStyle="bold"
            android:textSize="18sp"
            android:id="@+id/lifeline_toolbar_title"
            android:textColor="@android:color/white"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
    </android.support.v7.widget.Toolbar>
    <LinearLayout>
    </LinearLayout>
    <LinearLayout>
    </LinearLayout>
</LinearLayout>

Java Code

public class ll_home extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_ll_home);

        Toolbar toolbar = (Toolbar) findViewById(R.id.lifeline_toolbar);
        setSupportActionBar(toolbar);
        ActionBar actionBar = getSupportActionBar();
        actionBar.setDisplayHomeAsUpEnabled(true);
    }
}

You need to set the getSupportActionBar().setHomeButtonEnabled(true); enable the Up Action button

Toolbar toolbar = (Toolbar) findViewById(R.id.lifeline_toolbar);
setSupportActionBar(toolbar);
ActionBar actionBar = getSupportActionBar();
actionBar.setDisplayHomeAsUpEnabled(true);
actionBar.setHomeButtonEnabled(true);

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