【问题标题】:Extended Toolbar with Custom View not Displaying with Full Width具有自定义视图的扩展工具栏不以全宽显示
【发布时间】: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


【解决方案1】:

因此,为了实现这一点并完全控制工具栏中的填充,我创建了两个工具栏。第一个标准高度为 56dp 的工具栏和第二个高度为 72dp 的工具栏一起构成了 Material Design 指定的双层工具栏。

而且因为我没有在第二个工具栏中膨胀任何菜单项,所以我里面的所有自定义视图都按预期运行。

这些行仍然需要使用

    app:contentInsetStart="0dp"
    app:contentInsetEnd="0dp"
    app:contentInsetLeft="0dp"
    app:contentInsetRight="0dp"
    android:clipToPadding="false"

这解决了我的问题,所以现在我在我的 XMl 中包含了两个工具栏。

【讨论】:

  • 这是唯一对我有用的东西。感谢您的绝妙想法 - 将一个 ToolBar 设置为 supportActionBar(),并将另一个用于我们的自定义布局。
  • 谢谢你,它节省了我的时间~
【解决方案2】:

由于设计支持库的第 23 版,使用android.support.design.widget.CoordinatorLayout 实现它的方法要简单得多

这是一个例子:

<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:fitsSystemWindows="true">

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

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:popupTheme="@style/AppTheme.PopupOverlay">            
        </android.support.v7.widget.Toolbar>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@color/project_light_green"
            android:orientation="horizontal"
            android:paddingLeft="@dimen/activity_horizontal_margin_half"
            android:paddingRight="@dimen/activity_horizontal_margin_half"
            android:paddingTop="@dimen/activity_vertical_margin_half">

            <ImageView
                android:layout_width="24dp"
                android:layout_height="24dp"
                android:layout_margin="12dp"
                android:src="@drawable/ic_search_24dp" />

            <EditText
                android:id="@+id/search_field"
                android:layout_width="0dp"
                android:layout_height="48dp"
                android:layout_weight="1"
                android:background="@null"
                android:hint="@string/autocomplete_hint"
                android:singleLine="true"
                android:textColor="@color/project_black"
                android:textColorHint="@color/project_dark_gray"/>

            <include layout="@layout/close_button"
                android:id="@+id/clearButton"/>

        </LinearLayout>

    </android.support.design.widget.AppBarLayout>

    <include layout="@layout/content_search_location" />

</android.support.design.widget.CoordinatorLayout>

【讨论】:

  • 那里最有用的答案。
  • 一些解释:&lt;include layout="@layout/content_search_location" /&gt;
【解决方案3】:

来自Toolbar 文档:

一个或多个自定义视图。应用程序可以添加任意子 工具栏的视图。他们会出现在这个位置 布局。如果子视图的 Toolbar.LayoutParams 指示 Gravity CENTER_HORIZONTAL 的值,视图将尝试在 在所有其他元素完成后,工具栏中剩余的可用空间 被测量了。

因此,从外观上看,您的自定义视图的行为应如此。选项菜单占用了您工具栏中的一定空间,因此工具栏正在测量剩余空间以供您的自定义视图适应。

【讨论】:

  • 是的,好的。那讲得通。我想了一个办法绕过它。我会试一试,然后告诉你。而且我目前无法投票,因为我没有足够的代表:/
  • @JoeSovcik 你能解决这个问题吗?几个小时以来,我一直在思考这个问题!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-02-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多