Android: How to Center title in ToolBar

后端 未结 19 1919
执念已碎
执念已碎 2020-11-27 19:04

I am using ToolBar in my project first time, so i do not know how to customized the toolbar in android. I need to centered title in to the tool bar and how to do that please

相关标签:
19条回答
  • 2020-11-27 19:34

    you can insert this code to your code

    SupportActionBar.Title = "title";
    
    0 讨论(0)
  • 2020-11-27 19:35

    The best solution will be to use a nested TextView in the ToolBar then set the layout_width and layout_height to wrap_content. Then set the layout_gravity to center. This way it is not offset by other icons in the toolbar.

    0 讨论(0)
  • 2020-11-27 19:35

    This did my work!

    Toolbar toolbar = findViewById(R.id.mytoolbar);
            TextView titleText = getProperties(new TextView(this));
            FrameLayout frameLayout = new FrameLayout(this);
            frameLayout.setLayoutParams(new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.MATCH_PARENT));
            frameLayout.addView(titleText);
            toolbar.addView(frameLayout);
            setSupportActionBar(toolbar);
    

    Method to Set TextView Properties

     private TextView getProperties(TextView titleText){
            titleText.setText(wallpaperName);
            titleText.setTextColor(Color.WHITE);
            titleText.setTextSize(18f);
            titleText.setShadowLayer(2f,2f,2f,Color.BLACK);
            titleText.setTextAlignment(View.TEXT_ALIGNMENT_CENTER); // CENTER ALIGNMENT
            return titleText;
        }
    
    0 讨论(0)
  • 2020-11-27 19:37

    The problem with simply adding a TextView in the Toolbar aligned center is adding menu items in the toolbar which will offset the centered text.

    To get around this, I've layered the text on top of the Toolbar, not inside it. This way it doesn't matter how many icons you add, it will not offset the centered text:

    <android.support.design.widget.AppBarLayout
        android:id="@+id/appbar_layout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
    
            <android.support.v7.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:theme="@style/ThemeOverlay.AppCompat.ActionBar">
            </android.support.v7.widget.Toolbar>
    
            <TextView
                android:layout_centerInParent="true"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/app_name"/>
    
        </RelativeLayout>
    </android.support.design.widget.AppBarLayout>
    

    This way there is no need for any extra logic to compensate for the offset spacing of back buttons/overflow menu/search icons etc. on the toolbar, because the centered text is above it, not in it.

    0 讨论(0)
  • 2020-11-27 19:38

    When you have Home or Up button together with centered title, and the title isn't centered anymore and is moved slightly to the right a bit, set your textview as width=wrap_content and layout_gravity=center

    <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"/>
    
    0 讨论(0)
  • 2020-11-27 19:38

    Use custom title (with gravity center) inside toolbar

         <android.support.v7.widget.Toolbar           
             android:layout_width="match_parent"
             android:layout_height="?attr/actionBarSize"
             android:animateLayoutChanges="true"
             android:backgroundTint="@color/colorPrimary"
             android:background="?attr/colorPrimary"
             android:elevation="4dp"
             android:minHeight="?android:attr/actionBarSize"
             app:layout_collapseMode="pin"
             app:layout_scrollFlags="scroll|exitUntilCollapsed"
             app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
             app:popupTheme="@style/AppTheme.PopupOverlay"
             app:titleTextAppearance="@style/Toolbar.TitleText" >
    
          <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/toolbar_title"
                android:text="Title"
                android:layout_gravity="center"/>
    
         </android.support.v7.widget.Toolbar>
    
    0 讨论(0)
提交回复
热议问题