android - 带有自定义 View 的扩展工具栏未以全宽显示

标签 android android-layout android-custom-view android-toolbar android-actionbaractivity

我在这里浏览了很多与工具栏相关的答案,但没有一个答案对我有帮助。

我想要实现的是有一个扩展的工具栏,它将显示一个 Logo ,可能是 Activity/应用程序的名称,它将有一个 Action 按钮/抽屉切换到右侧,将显示类似导航的右边的抽屉,一个带有设置等其他选项的溢出菜单,底部还有一个导航选项卡,允许用户在不同的 fragment (一个月中的不同日期)之间移动。

我试图实现这一点的方法是通过工具栏。 首先,我创建了工具栏并添加了我的自定义 View 。

    <?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 只是一个水平 View ,带有两个填充为 72dp 的 ImageView (按照指南)和一个布局权重设置为 1 的 TextView ,因此它延伸到整个可用空间。高度为 72dp。

现在,在我的 BaseActivity XML 中,我将工具栏包含在主上下文的框架布局中(否则工具栏会由于我未知的原因覆盖整个屏幕(我花了很长时间才弄清楚)

<...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 上膨胀菜单,我的自定义 View 缩小并且不会扩展创建的选项的边缘。 下图更好地说明了这一点,它是 Android Studio 中设计 View 的屏幕截图。 黄色矩形表示将放置选项项的位置。 紫色表示 DesignView 中的原始大小。黑色表示运行应用程序时的大小。

Design View of the toolbar

感谢您的帮助。

可能对遇到此问题并希望解决类似问题的任何人有用的相关问题:

最佳答案

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

并且因为我没有在第二个工具栏中展开任何菜单项,所以我内部的所有自定义 View 都按预期运行。

虽然这些线仍然需要使用

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

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

关于android - 带有自定义 View 的扩展工具栏未以全宽显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29239254/

相关文章:

java - 无法使用 FCM 解析 Android 通知的符号 'NotificationChannel'

android - 在 Android Studio 上使用 Calendar.getInstance() 时如何插入和填充日期时间

android - getWindow setBackgroundDrawable Color.TRANSPARENT 使背景黑色

android - 水平对齐两个按钮

android - 自定义 View 能否获得类似 "onClose"或 "onDestroy"事件?

android - EditText getText().toString() 不工作

android - 无法连接到 android studio 中的 jcenter()

android - Kotlin 自定义 View 引用 textview 返回 null

android - 如何创建自定义评分栏

Android View inflateException