【问题标题】:Support Toolbar title/menu vertical centre支持工具栏标题/菜单垂直居中
【发布时间】:2016-01-01 11:30:54
【问题描述】:

我是 android 开发的新手,我正在开发一个带有 ActionBar 和 Tabs 的应用程序。最近我切换到了新的支持库Toolbar,但是工具栏(ActionBar)的标题不是垂直居中的,而操作按钮是正确放置的。 Title image

栏中的第一个 MenuItem 也会出现同样的问题(它向上移动了一点)。 Menu image

我的活动扩展了 AppCompatActivty,布局如下所示:

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
                                                 xmlns:app="http://schemas.android.com/apk/res-auto"
                                                 xmlns:tools="http://schemas.android.com/tools"
                                                 android:layout_width="match_parent"
                                                 android:layout_height="match_parent"
                                                 android:orientation="vertical"
                                                 tools:context=".MainActivity">

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <android.support.v7.widget.Toolbar
            android:id="@+id/action_bar_toolbar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:minHeight="?attr/actionBarSize"
            app:theme="@style/ActionBarTheme"/>

        <android.support.design.widget.TabLayout
            android:id="@+id/tabs"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@color/colorPrimary"
            app:tabGravity="fill"
            app:tabMaxWidth="0dp"
            app:tabMode="fixed"/>

        <com.myApp.app.views.widgets.ViewPager
            android:id="@+id/fragmentPager"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@android:color/white"/>
    </android.support.design.widget.AppBarLayout>
</android.support.design.widget.CoordinatorLayout>

风格:

 <style name="ActionBarTheme" parent="Widget.AppCompat.Toolbar">
        <item name="android:textColorPrimary">@color/colorPrimaryDark</item>
        <item name="colorControlNormal">@color/colorPrimaryDark</item>
        <item name="android:background">@color/colorPrimary</item>
  </style>

有人可以帮我解决这个问题吗?我一直在寻找解决方案,但一无所获。

附:当我使用默认操作栏时,它们被正确放置

【问题讨论】:

    标签: android android-toolbar


    【解决方案1】:

    您应该在这里使用android:theme 而不是app:theme

    android:theme="@style/ActionBarTheme"
    

    您的样式应该源自操作栏叠加层:

    <style name="ActionBarTheme" parent="ThemeOverlay.AppCompat.Dark.ActionBar">
    ...
    </style>
    

    考虑直接使用 ActionBar 主题来开始隔离问题。

    【讨论】:

      猜你喜欢
      • 2017-05-02
      • 1970-01-01
      • 2013-11-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-08-11
      相关资源
      最近更新 更多