android - 如何删除 'recycleview' 布局顶部的空间?

标签 android listview android-recyclerview

<?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"
    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="180dp"
        android:fitsSystemWindows="true">
            <android.support.v7.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="180dp"
                app:layout_scrollFlags="scroll|enterAlways">
                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="dddddddasdfaf2fdsafdd"/>
                </android.support.v7.widget.Toolbar>
    </android.support.design.widget.AppBarLayout>
    <android.support.v7.widget.RecyclerView
        android:id="@+id/recycler_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:clipToPadding="true"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"/>
</android.support.design.widget.CoordinatorLayout>

list_item.card.xml

<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
                                    xmlns:card_view="http://schemas.android.com/apk/res-auto"
                                    android:layout_width="match_parent"
                                    android:layout_height="80dp"
                                    card_view:cardElevation="5dp"
                                    card_view:cardMaxElevation="5dp"
                                    card_view:cardPreventCornerOverlap="true"
                                    card_view:cardUseCompatPadding="true">

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

        <ImageView
            android:id="@+id/thumbnail"
            android:layout_width="100dp"
            android:layout_height="80dp"
            android:layout_alignParentLeft="true"
            android:layout_centerVertical="true"
            android:background="#00ff00"
            android:scaleType="centerCrop"/>

        <TextView
            android:id="@+id/title"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_centerVertical="true"
            android:layout_toRightOf="@+id/thumbnail"
            android:maxLines="3"
            android:padding="8dp"
            android:text="dafdafda"
            android:textColor="#222"
            android:textSize="15dp"/>

    </RelativeLayout>
</android.support.v7.widget.CardView>

layoutmonitor captured image

我使用 CoordinatorLayout 和 RecyclerView 进行布局。构建和启动是成功的。但是我有一个小问题。正如您看到的捕获图像,recyclerview 上方有空间。我似乎是 recyclerview 的空间(顶部边距),但我从未定义边距或填充。

如何去除这个顶部空间边距?

最佳答案

我不知道为什么你的 AppBarLayout 和 Toolbar 中的 layout_height 设置了值..

但是,试试这段代码

<?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"
    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:theme="@style/MyTheme.AppBarOverlay">

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

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="dddddddasdfaf2fdsafdd" />
        </android.support.v7.widget.Toolbar>

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

    <android.support.v7.widget.RecyclerView
        android:id="@+id/recycler_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:clipToPadding="true"
        app:layout_behavior="@string/appbar_scrolling_view_behavior" />

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

查看结果

enter image description here

关于android - 如何删除 'recycleview' 布局顶部的空间?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36451375/

相关文章:

android - 如何下拉刷新 ListView 而不重复它的项目

java - 如何在回收者 View 中过滤数据

java - Android Studio Google Glass 模拟器中未检测到点击/滑动

运行时下载的自签名证书的 Android N 网络安全配置

java - 每次当我滚动随机项目时更改项目背景颜色时,项目也会更改背景颜色

qt - 如何根据内部内容使ListView项目高度动态化?

android - 在 android 回收器 View 中确定快照结束事件

android - RecyclerView 根本不更新

android - 使用 View 作为页面而不是 fragment 保存 viewpager 的状态

java - 我如何绘制从我的 android 应用程序发送到 tomcat 服务器的经度和纬度并在那里绘制谷歌地图?