具有自定义视图的扩展工具栏无法以全宽显示

Joe*_*cik 9 android android-custom-view android-layout android-actionbaractivity android-toolbar

我在这里找到了很多与工具栏相关的答案,但没有一个答案可以帮助我.

我想要实现的是有一个扩展的工具栏,它将显示一个徽标,可能是一个活动/应用程序的名称,它将有一个动作按钮/抽屉切换到右侧,将显示一个导航般的抽屉到右边是一个溢出菜单,其中包含其他选项,如设置和底部的导航标签,允许用户在不同的片段(一个月中的不同日期)之间移动.

我试图实现这一点的方式是通过工具栏.首先,我创建工具栏并添加我的自定义视图.

    <?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>
Run Code Online (Sandbox Code Playgroud)

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>
Run Code Online (Sandbox Code Playgroud)

但是,当我使用工具栏时,我在onCreateOptionsMenu上的actionBar上对菜单进行了充气,​​我的自定义视图会缩小,并且不会扩展所创建选项的边缘.下面的图片说明了更好,它是来自Android Studio中的设计视图的屏幕截图.黄色矩形表示将放置选项的位置.紫色表示DesignView中的原始大小.黑色表示运行应用程序时的大小.

工具栏的设计视图

感谢您的任何帮助.

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

Joe*_*cik 17

因此,为了实现这一点并完全控制工具栏中的填充,我创建了两个工具栏.第一个工具栏标准高度为56dp,第二个工具栏高度为72dp,它们共同构成了一个双层工具栏,由材料设计指定.

因为我没有给第二个工具栏中的任何菜单项充气,所以我所有的cusotm视图都按照需要运行.

这些线仍然需要使用

    app:contentInsetStart="0dp"
    app:contentInsetEnd="0dp"
    app:contentInsetLeft="0dp"
    app:contentInsetRight="0dp"
    android:clipToPadding="false"
Run Code Online (Sandbox Code Playgroud)

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


Tom*_*per 5

从设计支持库的第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>
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述