Reputation: 11999
I am using toolbar with in my app
This is my code
toolbar = (Toolbar) findViewById(R.id.tool_bar);
setSupportActionBar(toolbar);
My XML
<android.support.v7.widget.Toolbar
android:id="@+id/tool_bar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/appColor"
android:minHeight="?attr/actionBarSize"
app:theme="@style/MyDarkToolbarStyle" />
Styles
<style name="MyDarkToolbarStyle" parent="Widget.AppCompat.Toolbar">
<item name="android:gravity">center_vertical</item>
<item name="popupTheme">@style/PopupMenuStyle</item>
</style>
<style name="PopupMenuStyle" parent="Base.ThemeOverlay.AppCompat.ActionBar">
<item name="android:textColorPrimary">@color/black</item>
<item name="android:gravity">left|center</item>
<item name="android:background">@color/white</item>
</style>
This is how it looks
As you can see the the title text is not aligned either the navigation drawer
Upvotes: 0
Views: 1454
Reputation: 15547
I had the following on the toolbar widget already:
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
I ended up adding the following to the toolbar style:
<item name="android:gravity">center_vertical</item>
This allowed my title to be centered vertically properly.
Upvotes: 1
Reputation: 6063
I have also same problem , it solve by using
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
For more details read issues on https://code.google.com/p/android/issues/detail?id=77874
Upvotes: 3
Reputation: 10038
I use this, i put my own TextView and style it as i wish:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/tools"
android:id="@+id/materialToolbar"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:minHeight="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:contentInsetLeft="0dp"
app:contentInsetStart="0dp"
app:shadowDrawable="@drawable/header_shadow"
app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light">
<!--app:theme declaration will make sure that your text and items are using solid colors-->
<TextView
android:id="@+id/toolbar_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="0dp"
android:layout_marginStart="16dp"
android:layout_gravity="center"
android:text="..."
android:textColor="@color/white"
android:textSize="18sp"
android:focusable="false" />
</android.support.v7.widget.Toolbar>
Then inside Activity
, i hide default title. I also change typeface too:
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setTitle(null);
toolbarTitle = (TextView) toolbar.findViewById(R.id.toolbar_title);
//toolbarTitle.setTypeface(((Itu) getApplication()).getToolbarTypeFace());
toolbarTitle.setText(R.string.title_activity_course_list);
Upvotes: 1