操作栏标题没有正确对齐,android?

时间:2015-04-08 10:01:38

标签: android android-actionbar toolbar

我在我的应用中使用工具栏

这是我的代码

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

如您所见,标题文字未与导航抽屉对齐

3 个答案:

答案 0 :(得分:2)

我也有同样的问题,它通过使用

来解决
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"

有关详细信息,请阅读相关问题 https://code.google.com/p/android/issues/detail?id=77874

答案 1 :(得分: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); 

答案 2 :(得分:1)

我已经在工具栏小部件上有以下内容:

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

我最终将以下内容添加到工具栏样式中:

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

这使得我的标题可以垂直居中。