2015-04-08 5 views
0

Я использую панель с в моем приложенииНазвание панели действий не правильно выровнено, андроид?

Это мой код

toolbar = (Toolbar) findViewById(R.id.tool_bar); 
    setSupportActionBar(toolbar); 

Мой 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" /> 

Стили

<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> 

Вот как это выглядит

enter image description here

Как вы можете видеть текст заголовка не выравниваются либо панель навигации

+0

Работает ли он по умолчанию? – Ridcully

+0

Мне пришлось добавить в мой стиль следующее center_vertical. –

ответ

2

У меня есть также та же проблема, что решить с помощью

android:layout_width="match_parent" 
android:layout_height="?attr/actionBarSize" 

Для более подробной информации читайте вопросы https://code.google.com/p/android/issues/detail?id=77874

+0

Thnx ... это действительно работает – WISHY

+0

ваш самый добро пожаловать .. –

1

Я использую это, я положил свой собственный TextView и стиль его, как я хочу:

<?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> 

Тогда внутри Activity, я скрываю название по умолчанию. Я также изменить шрифт тоже:

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); 
1

я имел следующее на панели инструментов виджета уже:

android:layout_width="match_parent" 
android:layout_height="?attr/actionBarSize" 

В итоге я добавил следующее в стиле панели инструментов:

<item name="android:gravity">center_vertical</item> 

Это позволило моему заглавию правильно повернуть по центру.