【发布时间】:2015-10-28 05:19:35
【问题描述】:
我在这里查看了很多与工具栏相关的答案,但没有一个答案可以帮助我。
我想要实现的是有一个扩展的工具栏,它会显示一个徽标,可能是一个活动/应用程序的名称,它将有一个操作按钮/抽屉切换到右侧,将显示一个类似导航的右侧的抽屉,一个带有其他选项(如设置)的溢出菜单,底部还有一个导航选项卡,允许用户在不同的片段之间移动(一个月中的不同天)。
我试图实现这一点的方式是通过工具栏。 首先,我创建工具栏并添加我的自定义视图。
<?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 中的原始大小。黑色表示运行应用时的大小。
感谢您的帮助。
可能对遇到此问题并希望解决类似问题的任何人有用的相关问题:
【问题讨论】:
-
如何将工具栏添加到活动中?你在用
setSupportActionBar(Toolbar)吗? -
@Lady_ari:是的,我正在这样做。还有其他方法吗?另外,如果我想添加一个标题和图标等,我应该在设置工具栏之前做,还是应该修改我调用 getSupportActionBar() 时得到的对象?
-
我觉得修改工具栏标题等还是用
getSupportActionBar()比较好 -
顺便说一下,您的工具栏所在的 FrameLayout 看起来高度为
match_parent。为什么?我认为这会使工具栏适合整个屏幕,不是吗?除非它在另一个布局中 -
@Lady_ari 它在抽屉布局里面。当工具栏位于框架布局之外时,它实际上适合整个屏幕,我不知道为什么这么久,我只是将它随机添加到框架布局中。然后一切都按预期工作。我怀疑抽屉布局与此有关。
标签: android android-layout android-custom-view android-toolbar android-actionbaractivity