簡體   English   中英

協調器布局,工具欄頂部空白背景

[英]Coordinator Layout, Toolbar top margin weird background

我正在嘗試實現需要縮小卡片視圖的CoordinatorLayout 稍后,我打算針對此卡視圖的展開和縮小狀態使用不同的布局。 我已經成功實現了空名片視圖,但是在該名片視圖的頂部留有奇怪的背景。

很難用語言解釋,所以這是圖片 在此處輸入圖片說明

這是此活動的XML外觀

<?xml version="1.0" encoding="utf-8"?>
<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:id="@+id/app_bar"
    android:layout_width="match_parent"
    android:layout_height="@dimen/app_bar_height"
    android:fitsSystemWindows="true"
    android:layout_margin="16dp">

    <android.support.design.widget.CollapsingToolbarLayout
        android:id="@+id/toolbar_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:elevation="4dp"
        android:fitsSystemWindows="true"
        app:contentScrim="?attr/colorPrimary"
        app:layout_scrollFlags="scroll|exitUntilCollapsed"
        app:toolbarId="@+id/toolbar">

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

            <!--<android.support.v7.widget.CardView-->
                <!--android:layout_width="match_parent"-->
                <!--android:layout_height="match_parent"-->
                <!--android:background="@color/colorAccent"-->
                <!--android:layout_margin="8dp"/>-->
        </android.support.v7.widget.Toolbar>

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

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

<android.support.design.widget.FloatingActionButton
    android:id="@+id/fab"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_margin="@dimen/fab_margin"
    app:layout_anchor="@id/app_bar"
    app:layout_anchorGravity="bottom|end"
    app:srcCompat="@android:drawable/ic_dialog_email"/>

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

我希望此工具欄像卡片視圖一樣浮動。 讓我知道我是否需要更多細節。

您可以在Github上找到完整的源代碼。

您正在將layout_margin設置為AppBarLayout ,以設置此視圖的垂直和水平邊距(僅用於頂部,底部,右側和左側)。

如果要從特定側面設置邊距,則必須通過layout_marginLeftlayout_marginRightlayout_marginBottomlayout_marginTop手動設置邊距。

簡而言之,如果您想刪除上邊距。 然后只需手動將邊距設置為

<android.support.design.widget.AppBarLayout
    android:id="@+id/app_bar"
    android:layout_width="match_parent"
    android:layout_height="@dimen/app_bar_height"
    android:fitsSystemWindows="true"
    android:layout_marginLeft="16dp"
    android:layout_marginRight="16dp"
    android:layout_marginBottom="16dp">

       <!-- your components here -->

</android.support.design.widget.AppBarLayout>
  • 我最終在CoordinatorLayout中添加了相同的填充量

    android:paddingTop="16dp"

  • 將您的AppBarLayout中的android:layout_margin="16"更改為

     android:layout_marginLeft="16dp" android:layout_marginRight="16dp" android:layout_marginBottom="16dp" 

我已經測試過並且可以正常工作

暫無
暫無

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

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