【问题标题】:Custom Toolbar Views not centered自定义工具栏视图未居中
【发布时间】:2017-06-16 21:46:10
【问题描述】:

ActionBarDrawerToggle 与我的自定义Toolbar 一起使用时,Toolbar 中的TextViews 不再居中。

main_layout.xml

<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <include layout="@layout/toolbar" />

        <FrameLayout
            android:id="@+id/flContent"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_marginTop="?attr/actionBarSize"
            android:fitsSystemWindows="true" />
    </RelativeLayout>

    <android.support.design.widget.NavigationView
        android:id="@+id/nvView"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:background="@android:color/white"
        app:headerLayout="@layout/nav_header"
        app:menu="@menu/drawer_view" />

</android.support.v4.widget.DrawerLayout>

toolbar.xml

<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="?attr/actionBarSize"
    android:background="?attr/colorPrimaryDark"
    android:elevation="5dp"
    android:minHeight="?attr/actionBarSize"
    app:contentInsetLeft="0dp"
    app:contentInsetStart="0dp"
    app:contentInsetStartWithNavigation="0dp"
    app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            android:gravity="center_horizontal|center_vertical"
            android:orientation="vertical">

            <TextView
                android:id="@+id/tvNavTitle"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:background="@color/colorBackgroundBlack"
                android:gravity="center"
                android:textColor="@color/colorWhite"
                android:textSize="@dimen/text_size_large" />

            <TextView
                android:id="@+id/tvNavDate"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:background="@color/colorBackgroundBlack"
                android:gravity="center"
                android:textColor="@color/colorWhite"
                android:textSize="@dimen/text_size_small" />
        </LinearLayout>

        <ImageView
            android:layout_width="35dp"
            android:layout_height="35dp"
            android:layout_alignParentRight="true"
            android:layout_centerVertical="true"
            android:src="@mipmap/ic_launcher" />
    </RelativeLayout>

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

我尝试在Toolbar 上设置contentInsetStart 和其他属性,但没有任何改变。

【问题讨论】:

  • 到底有什么问题?你忘了提。
  • 汉堡包图标后面的空格。我需要全宽显示自定义工具栏
  • Toolbar 是全角的。这就是汉堡包图标所在的位置; Toolbar.
  • 那为什么“Home”文本不在中心?
  • 因为RelativeLayout在图标之后开始,其实就是ImageButton本身。您的TextViews 位于RelativeLayout 的中心。

标签: android xml toolbar


【解决方案1】:

这里的问题是ActionBarDrawerToggle 的图标设置为Toolbar 上的导航按钮。这个按钮是Toolbar 的一个特殊子元素,它将在布局中优先。添加到Toolbar 的任何其他子Views 将仅分配在放置ImageButton 之后剩余的空间。这会将RelativeLayout 的左侧推到右侧,因此您居中的TextViews 不会相对于Toolbar 本身居中。

幸运的是,ToolbarLayoutParams 有一个重力属性,我们可以利用它来将LinearLayout 和它的TextViews 直接放在Toolbar 中,而不必将它们包装在另一个ViewGroup 中.我们还可以在您的ImageView 上适当地设置重力,以同样将其与右侧对齐。

在本例中,我们通过将LinearLayoutlayout_gravity 设置为center 来应用该中心重心。确保还将layout_width 值更改为wrap_content,否则您将和以前一样。此处的ImageView 将其layout_gravity 设置为right|center_vertical,替换layout_* 特定于RelativeLayout 的属性。

<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="?attr/actionBarSize"
    android:background="?attr/colorPrimaryDark"
    android:elevation="5dp"
    android:minHeight="?attr/actionBarSize"
    app:contentInsetLeft="0dp"
    app:contentInsetStart="0dp"
    app:contentInsetStartWithNavigation="0dp"
    app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

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

        <TextView
            android:id="@+id/tvNavTitle"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textColor="@color/colorWhite"
            android:textSize="@dimen/text_size_large" />

        <TextView
            android:id="@+id/tvNavDate"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textColor="@color/colorWhite"
            android:textSize="@dimen/text_size_small" />

    </LinearLayout>

    <ImageView
        android:layout_width="35dp"
        android:layout_height="35dp"
        android:layout_gravity="right|center_vertical"
        android:src="@mipmap/ic_launcher" />

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

【讨论】:

    【解决方案2】:

    我有同样的问题,我用android:contentInset修复了

    试试这个代码:

    <android.support.design.widget.CoordinatorLayout
        xmlns:app="http://schemas.android.com/apk/res-auto" 
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    
        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent">
    
            <android.support.design.widget.AppBarLayout
                android:id="@+id/app_bar_layout"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:fitsSystemWindows="true">
    
                <android.support.v7.widget.Toolbar
                    android:id="@+id/toolbar"
                    android:layout_width="match_parent"
                    android:layout_height="?attr/actionBarSize"
                    android:background="@color/colorPrimary"
                    android:contentInsetEnd="50dp"
                    android:contentInsetLeft="50dp"
                    android:contentInsetRight="50dp"
                    android:contentInsetStart="50dp"
                    android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
                    app:contentInsetEnd="50dp"
                    app:contentInsetLeft="50dp"
                    app:contentInsetRight="50dp"
                    app:contentInsetStart="50dp"
                    app:popupTheme="@style/ThemeOverlay.AppCompat.Light">
    
                    <LinearLayout
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:gravity="center_horizontal"
                        android:layout_centerInParent="true"
                        android:orientation="vertical">
    
                        <TextView
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_gravity="center_vertical"
                            android:layout_marginLeft="5dp"
                            android:text="@string/app_name_short"
                            android:textColor="#fff"
                            android:textSize="20dp" />
    
                    </LinearLayout>
    
                </android.support.v7.widget.Toolbar>
    
            </android.support.design.widget.AppBarLayout>
    
    
            <FrameLayout
                android:id="@+id/main_fragment_container"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_below="@+id/app_bar_layout" />
    
    
        </RelativeLayout>
    
    </android.support.design.widget.CoordinatorLayout>
    

    【讨论】:

      猜你喜欢
      • 2016-01-18
      • 1970-01-01
      • 2011-06-27
      • 1970-01-01
      • 2011-04-20
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多