簡體   English   中英

具有自定義視圖的擴展工具欄無法以全寬顯示

[英]Extended Toolbar with Custom View not Displaying with Full Width

我在這里找到了很多與工具欄相關的答案,但沒有一個答案可以幫助我。

我想要實現的是有一個擴展的工具欄,它將顯示一個徽標,可能是一個活動/應用程序的名稱,它將有一個動作按鈕/抽屜切換到右側,將顯示一個導航般的抽屜到右邊是一個溢出菜單,其中包含其他選項,如設置和底部的導航標簽,允許用戶在不同的片段(一個月中的不同日期)之間移動。

我試圖實現這一點的方式是通過工具欄。 首先,我創建工具欄並添加我的自定義視圖。

    <?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中的原始大小。 黑色表示運行應用程序時的大小。

工具欄的設計視圖

感謝您的任何幫助。

相關問題可能對遇到此問題以解決類似問題的任何人有用:

因此,為了實現這一點並完全控制工具欄中的填充,我創建了兩個工具欄。 第一個工具欄標准高度為56dp,第二個工具欄高度為72dp,它們共同構成了一個雙層工具欄,由材料設計指定。

因為我沒有給第二個工具欄中的任何菜單項充氣,所以我所有的cusotm視圖都按照需要運行。

這些線仍然需要使用

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

這解決了我的問題所以現在我將兩個工具欄包含在我的XMl中。

從設計支持庫的第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>

在此輸入圖像描述

工具欄文檔:

一個或多個自定義視圖。 應用程序可能會向工具欄添加任意子視圖。 它們將出現在布局中的這個位置。 如果子視圖的Toolbar.LayoutParams指示CENTER_HORIZONTAL的重力值,則在測量完所有其他元素后,視圖將嘗試以工具欄中剩余的可用空間為中心。

因此,從它的外觀來看,您的自定義視圖的行為應該如此。 選項菜單占用了工具欄中的一定空間,因此工具欄會測量您的自定義視圖所適合的剩余空間。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM