如何在支持操作栏中添加后退按钮时在工具栏中心对齐文本视图

How to align textview in the center of toolbar while adding backbutton in support actionbar

我创建了自定义工具栏作为我 activity 的操作栏。我希望标题标签显示在工具栏的中央。

工具栏布局资源文件是,

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<android.support.v7.widget.Toolbar xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/my_toolbar_select"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="?attr/colorPrimary"
    app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
    android:weightSum="1">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:orientation="vertical">

        <ImageView
            android:id="@+id/button1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="right"
            android:layout_marginRight="15dp"
            android:layout_marginTop="20dp"
            android:src="@drawable/ic_menu_camera" />

            <TextView
                android:id="@+id/text1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:singleLine="true"
                android:text="sample text"
                android:textColor="@color/progressWhite"
                android:textSize="14sp" />

            <TextView
                android:id="@+id/text2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:singleLine="true"
                android:text="Heading"
                android:layout_gravity="center"
                android:textColor="@color/progressWhite"
                android:textSize="18sp"
                android:textStyle="bold" />

            <TextView
                android:id="@+id/text3"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center|center_horizontal"
                android:singleLine="true"
                android:text="second line heading"
                android:textColor="@color/progressWhite"
                android:textSize="12sp"
                android:layout_marginBottom="15dp"/>


    </LinearLayout>


</android.support.v7.widget.Toolbar>

我的 Activity 包含,

Toolbar toolbar = (Toolbar) findViewById(R.id.my_toolbar_select_seat);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayShowTitleEnabled(false);

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

我想将所有三个 TextView 对齐到工具栏的中心,但无法准确对齐到工具栏的中心。当我 运行 设备中的应用程序未正确对齐时。我该如何解决这个问题?

来自 Toolbar 文档:

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 Toolbar.LayoutParams indicates a Gravity value of CENTER_HORIZONTAL the view will attempt to center within the available space remaining in the Toolbar after all other elements have been measured.

所以您希望 center_horizontal 在将位于工具栏中的视图上,在本例中为 LinearLayout。您可以将其设置为 wrap_content,这样它将在工具栏中居中,只占用它需要的空间。然后将其 gravity 设置为 center_horizontal,使其内容居中。然后您可以删除所有子项的所有 gravity_ 属性,因为它们现在基于 LinearLayout 对齐。我调整了你的样本:

<android.support.v7.widget.Toolbar xmlns:app="http://schemas.android.com/apk/res-auto"
  android:id="@+id/my_toolbar_select"
  android:layout_width="match_parent"
  android:layout_height="wrap_content"
  android:background="?attr/colorPrimary"
  app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

    <LinearLayout
      android:gravity="center_horizontal"
      android:layout_gravity="center_horizontal"
      android:layout_width="wrap_content"
      android:layout_height="match_parent"
      android:orientation="vertical">

        <ImageView
          android:id="@+id/button1"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:src="@drawable/cmn_add" />

        <TextView
          android:id="@+id/text1"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:singleLine="true"
          android:text="sample text"
          android:textColor="@color/white"
          android:textSize="14sp" />

        <TextView
          android:id="@+id/text2"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:singleLine="true"
          android:text="Heading"
          android:textColor="@color/white"
          android:textSize="18sp"
          android:textStyle="bold" />

        <TextView
          android:id="@+id/text3"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:singleLine="true"
          android:text="second line heading"
          android:textColor="@color/white"
          android:textSize="12sp"
          android:layout_marginBottom="15dp"/>

    </LinearLayout>

</android.support.v7.widget.Toolbar>

这给了我这样的东西:

希望对您有所帮助!

P.S。 - 当 post 遇到有关布局看起来不正确的问题时,post 您正在获取的布局的屏幕截图以及模拟或您实际想要的东西会非常有用。否则人们不得不根据你的描述来猜测。