简体   繁体   English

协调器布局,工具栏顶部空白背景

[英]Coordinator Layout, Toolbar top margin weird background

I am trying to implement a CoordinatorLayout in which I need a shrinking card view. 我正在尝试实现需要缩小卡片视图的CoordinatorLayout I plan to have different layouts for expanded and shrunk state of this card view later on. 稍后,我打算针对此卡视图的展开和缩小状态使用不同的布局。 I have successfully implemented empty card view but I am getting a weird background in top margin of this card view. 我已经成功实现了空名片视图,但是在该名片视图的顶部留有奇怪的背景。

Its difficult to explain in words so here are the pictures 很难用语言解释,所以这是图片 在此处输入图片说明

Here is how my XML looks like for this activity 这是此活动的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>

I want this toolbar to float like a card view. 我希望此工具栏像卡片视图一样浮动。 Let me know if more details are needed from my end. 让我知道我是否需要更多细节。

You can find full source code on Github Here . 您可以在Github上找到完整的源代码。

You are setting layout_margin to your AppBarLayout which sets margin for this view vertically as well as horizontally (simply for top, bottom, right and left). 您正在将layout_margin设置为AppBarLayout ,以设置此视图的垂直和水平边距(仅用于顶部,底部,右侧和左侧)。

If you want to set margin from a specific side then you have to set margins manually by layout_marginLeft , layout_marginRight , layout_marginBottom and layout_marginTop . 如果要从特定侧面设置边距,则必须通过layout_marginLeftlayout_marginRightlayout_marginBottomlayout_marginTop手动设置边距。

So simply, if you want to remove top margin. 简而言之,如果您想删除上边距。 Then simply set margins manually as, 然后只需手动将边距设置为

<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>
  • I ended up adding the same amount padding to the CoordinatorLayout 我最终在CoordinatorLayout中添加了相同的填充量

    android:paddingTop="16dp"

  • change android:layout_margin="16" in your AppBarLayout to 将您的AppBarLayout中的android:layout_margin="16"更改为

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

I have tested it and it works 我已经测试过并且可以正常工作

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM