【问题标题】:Android centre title with navigation drawer带有导航抽屉的 Android 中心标题
【发布时间】:2015-11-12 04:30:20
【问题描述】:

我目前有一个带有标题的工具栏的导航抽屉,我希望将这个标题放在工具栏中,但工具栏似乎没有考虑到抽屉图标,如下图所示。

而当我对不在导航抽屉内的其他活动使用相同的工具栏布局时,标题完美居中,如下图所示:

那么我如何让它考虑到这个图标呢?

这是我的布局:

<android.support.design.widget.AppBarLayout
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_height="wrap_content"
    android:layout_width="match_parent"
    android:theme="@style/Theme.App.AppBarOverlay">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="@color/black"
        app:popupTheme="@style/Theme.App.PopupOverlay">

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <TextView
                android:id="@+id/toolbar_title"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerInParent="true"
                android:textColor="@color/white"
                android:textStyle="bold"
                android:text="Title"
                android:textSize="16sp" />

        </RelativeLayout>

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

</android.support.design.widget.AppBarLayout>

【问题讨论】:

    标签: android android-toolbar navigation-drawer


    【解决方案1】:

    您需要了解 Toolbar 小部件扩展了 ViewGroup 类,并且它有自己的 LayoutParams

    所以你不需要在 Toolbar 中使用 RelativeLayout,只需要在 TextView 中添加一行。

    android:layout_gravity="center_horizontal"
    

    最终的 xml 应该是这样的,

    <android.support.design.widget.AppBarLayout xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:theme="@style/Theme.App.AppBarOverlay" >
    
    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="@color/black"
        app:popupTheme="@style/Theme.App.PopupOverlay" >
    
        <TextView
            android:id="@+id/toolbar_title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:text="Title"
            android:textColor="@color/white"
            android:textSize="16sp"
            android:textStyle="bold" />
    </android.support.v7.widget.Toolbar>
    

    【讨论】:

    • 感谢@Chitrang 你让我开心
    • 我需要在 Toolbar 中显示 imageview 和 TextView,我该怎么做
    • 如果我必须强制使用 RelativeLayout 怎么办。因为我在工具栏中还有 2 张图片?
    【解决方案2】:

    删除RelativeLayout,然后像这样更改您的TextView

    <android.support.design.widget.AppBarLayout
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        android:theme="@style/Theme.App.AppBarOverlay">
    
        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="@color/black"
            app:popupTheme="@style/Theme.App.PopupOverlay">
    
            <TextView
                android:id="@+id/toolbar_title"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:text="Title"
                android:textColor="@color/white"
                android:textSize="16sp"
                android:textStyle="bold"/>
    
        </android.support.v7.widget.Toolbar>
    
    </android.support.design.widget.AppBarLayout>
    

    在调用 setDisplayShowTitleEnabled 之后,在您的活动的 onCreate 方法中删除原始标题:

    public class MainActivity extends AppCompatActivity {
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
            setSupportActionBar(toolbar);
            getSupportActionBar().setDisplayShowTitleEnabled(false);
    
            ...
        }
    }
    

    这是得到的结果:

    【讨论】:

      【解决方案3】:

      你需要在Textview中把android:layout_width="wrap_content"改成android:layout_width="match_parent".....

      【讨论】:

      • 所有这一切都是将文本视图拉伸到工具栏的宽度,正如我强调的那样,它不考虑图标,所以它仍然不在中心
      • match_parent 并将其重力(不是 layout_gravity)设置为中心
      • @NguyễnHoàiNam 再次这并不能解决偏移的问题,因为整个布局没有考虑抽屉按钮的宽度。
      • 啊。我认为导航按钮有一个你无法超越的空间。您应该阅读工具栏的源代码以获取更多信息和想法。
      【解决方案4】:

      希望这会有所帮助。尝试将此代码使用到您的 xml 中,您的代码只需少量更改:

      <TextView
              android:id="@+id/toolbar_title"
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
              android:layout_gravity="center_horizontal"
              android:text="title"
              android:gravity="center"
              android:textColor="#ffffff"
              android:textSize="21dp"
              android:textStyle="bold"
              android:paddingRight="30dp"
              android:paddingLeft="10dp"
              android:paddingTop="15dp" />
      

      将其用于每个活动。 这将确保您的文本将覆盖图标旁边的整个空间并将文本居中。

      <android.support.v7.widget.Toolbar android:id="@+id/toolbar"
          android:layout_width="match_parent" android:layout_height="?attr/actionBarSize"
          android:background="?attr/colorPrimary" app:popupTheme="@style/AppTheme.PopupOverlay" />
      <RelativeLayout
          android:layout_width="match_parent"
          android:layout_height="wrap_content">
      
          <ImageView
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:id="@+id/imageView"
              android:paddingTop="15dp"
              android:src="@drawable/ic_keyboard_arrow_left_black_24dp"
              android:layout_alignParentTop="true"
              android:layout_alignParentLeft="true"
              android:layout_alignParentStart="true" />
      
          <TextView
              android:id="@+id/toolbar_title"
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
              android:layout_gravity="center_horizontal"
              android:text="title"
              android:gravity="center"
              android:textColor="#ffffff"
              android:textSize="21dp"
              android:textStyle="bold"
              android:paddingRight="30dp"
              android:paddingLeft="10dp"
              android:paddingTop="15dp" />
      
      </RelativeLayout>
      
      --------Nav_Drawer layout 
      
          <?xml version="1.0" encoding="utf-8"?>
          <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:layout_width="match_parent" android:layout_height="@dimen/nav_header_height"
              android:background="@drawable/side_nav_bar"
              android:paddingBottom="@dimen/activity_vertical_margin"
              android:paddingLeft="@dimen/activity_horizontal_margin"
              android:paddingRight="@dimen/activity_horizontal_margin"
              android:paddingTop="@dimen/activity_vertical_margin"
              android:theme="@style/ThemeOverlay.AppCompat.Dark" android:orientation="vertical"
              android:gravity="bottom">
      
              <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content"
                  android:paddingTop="@dimen/nav_header_vertical_spacing"
                  android:src="@android:drawable/sym_def_app_icon" android:id="@+id/imageView" />
      
              <TextView android:layout_width="match_parent" android:layout_height="wrap_content"
                  android:paddingTop="@dimen/nav_header_vertical_spacing" android:text="Android Studio"
                  android:textAppearance="@style/TextAppearance.AppCompat.Body1" />
      
              <TextView android:layout_width="wrap_content" android:layout_height="wrap_content"
                  android:text="android.studio@android.com" android:id="@+id/textView" />
      
          </LinearLayout>
      

      【讨论】:

        【解决方案5】:

        对我来说,这两个答案都不起作用。相反,我只是将 -40dp 的边距开始放在 TextView 中,这是抽屉的图标宽度。而且效果很好。 也许它对某人有用

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2013-06-14
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2017-05-11
          • 1970-01-01
          相关资源
          最近更新 更多