Toolbar title not in center when Back Button is enable

前端 未结 6 719
名媛妹妹
名媛妹妹 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: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.

提交回复
热议问题