我在这里浏览了很多与工具栏相关的答案,但没有一个答案可以帮助我。
我想要实现的是拥有一个扩展工具栏,它将显示一个徽标,可能是活动/应用程序的名称,它将在右侧有一个操作按钮/抽屉切换开关,它将显示一个类似导航的抽屉右侧是一个带有其他选项(如设置)的溢出菜单,以及底部的导航选项卡,允许用户在不同的片段(一个月中的不同日期)之间移动。
我试图实现这一目标的方法是通过工具栏。
首先,我创建工具栏并添加自定义视图。
<?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/apk/res-auto"
android:id="@+id/toolbar_actionbar"
android:layout_width="match_parent"
android:layout_height="@dimen/min_double_toolbar_height"
app:theme="@style/ThemeOverlay.AppCompat.ActionBar"
android:minHeight="?attr/actionBarSize"
android:background="@color/primary"
app:contentInsetStart="0dp"
app:contentInsetEnd="0dp"
app:contentInsetLeft="0dp"
app:contentInsetRight="0dp"
android:clipToPadding="false"
>
<!-- ThemeOVerlay Makes sure the text and items are using solid colors-->
<include
android:layout_width="match_parent"
android:layout_height="72dp"
android:layout_gravity="bottom"
layout="@layout/day_navigation_tab"
/>
</android.support.v7.widget.Toolbar>
day_navigation_tab 只是一个水平视图,有两个图像视图,填充为 72dp(按照指南)和一个文本视图,布局权重设置为 1,因此它会延伸到整个可用空间。高度为72dp。
现在,在我的 BaseActivity XML 中,我将工具栏包含在主上下文的 Framelayout 中(否则工具栏将覆盖整个屏幕,原因不明(我花了很长时间才弄清楚))
<...ommited>
<FrameLayout
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent">
<include
android:id="@+id/toolbar"
layout="@layout/app_bar"
/>
</FrameLayout>
<!-- android:layout_gravity="start" tells DrawerLayout to treat
this as a sliding drawer on the left side for left-to-right
languages and on the right side for right-to-left languages.
If you're not building against API 17 or higher, use
android:layout_gravity="left" instead. -->
<!-- The drawer is given a fixed width in dp and extends the full height of
the container. -->
<fragment android:id="@+id/navigation_drawer"
android:layout_width="@dimen/navigation_drawer_width"
android:layout_height="match_parent"
android:layout_gravity="right"
android:name="alterway.agency.timeentry.NavigationDrawerFragment"
tools:layout="@layout/fragment_navigation_drawer"
app:layout="@layout/fragment_navigation_drawer"
/>
</android.support.v4.widget.DrawerLayout>
但是,当我使用工具栏时,我在 onCreateOptionsMenu 中的 actionBar 上膨胀菜单,我的自定义视图会缩小,并且不会扩展创建的选项的边缘。
下面的图片更好地说明了这一点,它是 Android Studio 中设计视图的屏幕截图。
黄色矩形表示选项项的放置位置。
紫色表示 DesignView 中的原始尺寸。黑色表示运行应用程序时的大小。
感谢您的任何帮助。
相关问题可能对任何遇到此问题并希望解决类似问题的人有用:
- 设计 - 支持工具栏自定义视图不使用全宽 https://stackoverflow.com/questions/27007985/support-toolbars-custom-view-not-using-full-width
- 如何在 Android L 上设置扩展工具栏 https://stackoverflow.com/questions/26491689/how-do-i-declare-an-extended-height-toolbar-action-bar-on-android-lollipop