繁体   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