如何删除带有圆形背景的AppBarLayout角落的空白

问题描述

我想让toolbar的底边缘具有拐角半径。我为此孔创建了一个drawable背景。但这是有问题的。工具栏的底角带有空格。我为layout和AppBarLayout的根设置了相同的背景色,但是在屏幕上滚动时仍显示空白。我尝试使用其他解决方案,例如为AppBarLayout设置此样式,但是不起作用。

  <!-- style appbar home -->
<style name="appbar_style" parent="ThemeOverlay.AppCompat.ActionBar">
    <item name="windowActionBarOverlay">true</item>
    <item name="android:windowActionBarOverlay">true</item>
</style>

这是工具栏背景的drawable

 <shape android:shape="rectangle">
        <gradient
            android:type="linear"
            android:angle="0"
            android:startColor="#EA4D13"
            android:endColor="#FD7920" />

        <corners
            android:bottomLefTradius="15dp"
            android:bottomrighTradius="15dp"/>
    </shape>

这是我的片段布局:

  <?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layoutDirection="rtl">

    <com.google.android.material.appbar.AppBarLayout
        android:id="@+id/app_bar_layout"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="@color/grey8"
        android:fitsSystemWindows="true">

        <androidx.appcompat.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="@drawable/drw_bg_toolbar_orange"
            app:contentInsetEnd="0dp"
            app:contentInsetStart="0dp"
            app:layout_collapseMode="pin"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:popupTheme="@style/ThemeOverlay.AppCompat.Light">

            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent">

                <ImageView
                    android:id="@+id/img_search"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignParentLeft="true"
                    android:layout_centerVertical="true"
                    android:layout_marginLeft="20dp"
                    android:src="@drawable/ic_search" />

                <ImageView
                    android:id="@+id/img_instagram"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignParentRight="true"
                    android:layout_centerVertical="true"
                    android:layout_marginRight="20dp"
                    android:src="@drawable/ic_instagram" />

                <TextView
                    android:id="@+id/txt_title"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_centerVertical="true"
                    android:layout_toLeftOf="@+id/img_instagram"
                    android:layout_toRightOf="@+id/img_search"
                    android:gravity="center"
                    android:text="@string/home_title"
                    android:textColor="@color/white" />
            </RelativeLayout>
        </androidx.appcompat.widget.Toolbar>
    </com.google.android.material.appbar.AppBarLayout>

    <androidx.swiperefreshlayout.widget.SwipeRefreshLayout
        android:id="@+id/swipe_container"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior">

        <androidx.constraintlayout.widget.ConstraintLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@color/grey8">

            <androidx.recyclerview.widget.RecyclerView
                android:id="@+id/rcy_hot_post"
                android:layout_width="match_parent"
                android:layout_height="0dp"
                app:layout_constraintBottom_toTopOf="@+id/img_retry"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toTopOf="parent" />

            <ImageView
                android:id="@+id/img_retry"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="?attr/selectableItemBackgroundBorderless"
                android:clickable="true"
                android:focusable="true"
                android:src="@drawable/ic_refresh"
                android:visibility="gone"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintVertical_bias="0.0" />

            <ProgressBar
                android:id="@+id/progressbar"
                style="@style/Widget.MaterialComponents.ProgressIndicator.Circular.Indeterminate"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:visibility="gone"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toTopOf="parent" />
        </androidx.constraintlayout.widget.ConstraintLayout>
    </androidx.swiperefreshlayout.widget.SwipeRefreshLayout>
</androidx.coordinatorlayout.widget.CoordinatorLayout>

output

解决方法

尝试将AppBarLayout的背景设置为@android:color / transparent

,

您应将android:layout_marginTop="-15"添加到SwipeRefreshLayout,然后添加一个空白视图,该视图的值为Height,与您在{{1} }

类似这样的东西:

android:layout_marginTop="15"

,

改善@mohammad的答案。您必须更改CoordinatorLayout组件的顺序。

检查:

<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layoutDirection="rtl">


    <androidx.swiperefreshlayout.widget.SwipeRefreshLayout
        android:id="@+id/swipe_container"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginTop="-30dp"    // it's better to set the same value as the corner radius used in the header shape.
        android:paddingTop="30dp" // same here,use the identical values to maintain the same height.
        app:layout_behavior="@string/appbar_scrolling_view_behavior">

     // other code.. 

    </androidx.swiperefreshlayout.widget.SwipeRefreshLayout>


    <com.google.android.material.appbar.AppBarLayout
        android:id="@+id/app_bar_layout"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="@color/grey8"
        android:fitsSystemWindows="true">

     // other code.. 

    </com.google.android.material.appbar.AppBarLayout>

</androidx.coordinatorlayout.widget.CoordinatorLayout>

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...