The problem here is that ActionBarDrawerToggle
's icon is set as the navigation button on the Toolbar
. This button is a special child of Toolbar
that will take precedence in the layout. Any other child View
s added to the Toolbar
will be allotted only the space remaining after that ImageButton
is placed. This is pushing the left side of your RelativeLayout
to the right, so the TextView
s you have centered in that will not be centered with respect to the Toolbar
itself.
Fortunately, Toolbar
's LayoutParams
has a gravity property that we can utilize to center the LinearLayout
and its TextView
s directly in the Toolbar
, without having to wrap them in another ViewGroup
. We can also set the gravity appropriately on your ImageView
to similarly align that to the right side.
In this example, we apply that center gravity by setting the LinearLayout
's layout_gravity
to center
. Be sure to also change the layout_width
values to wrap_content
, or you'll be in the same boat as before. The ImageView
here has its layout_gravity
set to right|center_vertical
, replacing those layout_*
attributes specific to 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>
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…