Toolbar title not in center when Back Button is enable

前端 未结 6 655
名媛妹妹
名媛妹妹 2021-02-02 13:53

I\'m trying to display my toolbar title in the center and to do it I use the method which is given in this answer :-Toolbar Center title

However, when I enable back butt

相关标签:
6条回答
  • 2021-02-02 14:00

    Having a placeholder image the same size as the back arrow and setting it to be invisible when the back arrow is not shown and gone when it's displayed did the trick for me.

    <?xml version="1.0" encoding="utf-8"?>
    <android.support.v7.widget.Toolbar
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/blue"
        android:minHeight="?attr/actionBarSize"
        app:contentInsetEnd="0dp"
        app:contentInsetLeft="0dp"
        app:contentInsetRight="0dp"
        app:contentInsetStart="0dp"
        app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
        app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
    
    <ImageView
        android:id="@+id/iv_placeholder"
        android:layout_width="72dp"
        android:layout_height="48dp"
        android:src="@drawable/ic_actionbar_hamburger"
        android:visibility="invisible"/>
    
    
    <TextView
        android:id="@+id/logo_tv"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginEnd="?attr/actionBarSize"
        android:gravity="center"
        android:text=""
        android:textColor="@color/white"
        android:textStyle="normal"/>
    
    </android.support.v7.widget.Toolbar>
    

    0 讨论(0)
  • 2021-02-02 14:02

    Add a TextView inside the Toolbar & don't forget to set the following attribute inside your TextView.

    android:layout_marginRight="?android:attr/actionBarSize"

    OR

    android:layout_marginEnd="?android:attr/actionBarSize"

    code snippet:

    <android.support.v7.widget.Toolbar
        android:id="@+id/custom_toolbar"
        android:layout_width="match_parent"
        android:layout_height="?android:attr/actionBarSize"
        android:background="@android:color/holo_red_dark">
        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="abc"
            android:textColor="@android:color/white"
            android:textSize="20sp"
            android:layout_marginRight="?android:attr/actionBarSize"
            android:gravity="center"/>
    
    </android.support.v7.widget.Toolbar>
    

    Refer to this tutorial for more information.

    0 讨论(0)
  • 2021-02-02 14:09

    Dont set propterties like this

    mActionBar = getSupportActionBar();
    mActionBar.setDisplayHomeAsUpEnabled(true);
    mActionBar.setDisplayShowHomeEnabled(true);
    

    Do like this

            Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
            // Title and subtitle
            toolbar.setTitle(R.string.about_toolbar_title);
            toolbar.setSubtitleTextColor(Color.WHITE);
            toolbar.setTitleTextColor(Color.WHITE);
            toolbar.setBackgroundColor(getResources().getColor(
                    R.color.themeToolbarColor));
            toolbar.setNavigationIcon(R.drawable.ic_action_back);
            toolbar.setNavigationOnClickListener(new OnClickListener() {
    
                @Override
                public void onClick(View v) {
                   finish();  // to go back  finish() will do your work.
                    //mActionBar.setDisplayHomeAsUpEnabled(true);
                    //mActionBar.setDisplayShowHomeEnabled(true);
                }
            });
    
    0 讨论(0)
  • 2021-02-02 14:11

    Just put your content in a child view inside the Toolbar tag in XML, using the following attributes:

    android:layout_width="wrap_content"
    android:layout_gravity="center"
    

    Offical docs for Toolbar state:

    One or more custom views. The application may add arbitrary child views to the Toolbar. They will appear at this position within the layout. If a child view's LayoutParams indicates a Gravity value of Gravity#CENTER_HORIZONTAL the view will attempt to center within the available space remaining in the Toolbar after all other elements have been measured.

    This works for me, using androidx.appcompat.widget.Toolbar with a child view.

    0 讨论(0)
  • 2021-02-02 14:16

    The reason why the title is not centered when you use a back button as navigation icon, is that navigation icon is represented as AppCompatImageButton and is added to the same layout as your title TextView. Using Arshak's answer is not a bad idea, but ?android:attr/actionBarSize is not a good way to define the end margin. As the action bar height is probably the same size as icon's width, it might work, but might not work on all devices. Could be a good idea to specify this size from material design guidelines.

    0 讨论(0)
  • 2021-02-02 14:18

    Just add android:paddingEnd="72dp; to the Toolbar layout.

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?android:attr/actionBarSize"
        app:contentScrim="@color/colorPrimary"
        app:layout_collapseMode="pin"
        android:paddingEnd="72dp"
        app:popupTheme="@style/ThemeOverlay.AppCompat.Dark"
        app:title="Title"/>
    
    0 讨论(0)
提交回复
热议问题