【发布时间】: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的中心。