【问题标题】:Recyclerview first item start marginRecyclerview 第一项起始边距
【发布时间】:2018-08-22 06:27:38
【问题描述】:

如何在启动片段时将第一项与tvFoo 对齐(不将RecyclerView 放入另一个容器中)?

现在,tvFoo 距离左边缘 16dp 和列表中的第一项,只有 8dp。

我想要的是水平列表在滚动时在屏幕上完全可见(但一开始它距离左侧 16dp)。

这就是我所拥有的

这就是我想要的

fragment_foo.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
    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="wrap_content">

    <TextView
        android:id="@+id/tvFoo"
        style="@style/Tv.16.Black"
        android:layout_marginStart="16dp"
        android:layout_marginTop="16dp"
        android:text="@string/foo"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"/>

    <TextView
        android:id="@+id/tvCount"
        style="@style/Tv.13.Gray"
        android:layout_marginStart="4dp"
        app:layout_constraintBottom_toBottomOf="@id/tvFoo"
        app:layout_constraintStart_toEndOf="@id/tvFoo"
        app:layout_constraintTop_toTopOf="@id/tvFoo"
        tools:text="(21)"/>

    <android.support.v7.widget.RecyclerView
        android:id="@+id/rv"
        style="@style/WrapContent"
        android:layout_marginTop="16dp"
        app:layout_constraintTop_toBottomOf="@id/tvFoo"
        tools:layoutManager="android.support.v7.widget.GridLayoutManager"
        tools:listitem="@layout/item_foo"
        tools:orientation="horizontal"/>

    <View
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:layout_marginTop="16dp"
        android:background="@color/colorPrimaryLight"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintTop_toBottomOf="@id/rv"/>
</android.support.constraint.ConstraintLayout>

item_foo.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
    style="@style/WrapContent"
    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_marginStart="8dp"
    android:background="?selectableItemBackground">

    <ImageView
        android:id="@+id/ivFoo"
        android:layout_width="160dp"
        android:layout_height="90dp"
        android:background="@color/lightGray"
        android:padding="16dp"
        android:src="@drawable/ic_picture_64dp"/>
</android.support.constraint.ConstraintLayout>

【问题讨论】:

  • Gabriel,因为你的问题是关于用户界面的,你为什么不用图片来展示你有什么,你想要完成什么?
  • 与左边框有关,@PriyankVadariya。
  • 好的,你能上传你想要的图片吗?根据@Alessio 评论,我们可以为您提供帮助
  • 谢谢你,@Alessio。完成!

标签: android android-layout android-recyclerview


【解决方案1】:

padding 添加到您的 RecyclerView 并使用

android:clipToPadding = "false"

在您的RecyclerView 中,在滚动时禁用填充。

<android.support.v7.widget.RecyclerView
        android:id="@+id/rv"
        style="@style/WrapContent"
        android:layout_marginTop="16dp"     
        android:padding="16dp"
        android:clipToPadding = "false"
        app:layout_constraintTop_toBottomOf="@id/tvFoo"
        tools:layoutManager="android.support.v7.widget.GridLayoutManager"
        tools:listitem="@layout/item_foo"
        tools:orientation="horizontal"/>

【讨论】:

  • 完美。唯一需要考虑的是将android:padding="16dp" 更改为android:padding="8dp",因为我在项目上有android:layout_marginStart="8dp"
  • 我总是忘记clipToPadding... :-D
  • 你拯救了我的一天
猜你喜欢
  • 2016-09-02
  • 1970-01-01
  • 2021-08-26
  • 2016-02-18
  • 2017-01-04
  • 1970-01-01
  • 1970-01-01
  • 2019-05-12
  • 2017-07-11
相关资源
最近更新 更多