【问题标题】:Placing horizontal recyclerview at a specific position using ConstraintLayout使用 ConstraintLayout 将水平回收视图放置在特定位置
【发布时间】:2019-02-21 20:23:38
【问题描述】:

我想将我的horizo​​ntl recyclerview 从一开始就放置在30% 的屏幕宽度处

<android.support.constraint.ConstraintLayout 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.support.v7.widget.RecyclerView
        android:id="@+id/recycler"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:clipToPadding="false"
        android:orientation="horizontal"
        android:paddingStart="20dp"
        android:paddingEnd="20dp"
        android:background="@android:color/white"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toEndOf="@id/guideline" />

    <android.support.constraint.Guideline
        android:id="@+id/guideline"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        app:layout_constraintGuide_percent="0.3" />

</android.support.constraint.ConstraintLayout>

但它不是 recyclerview 没有出现在屏幕上工作任何想法如何实现这一点。

【问题讨论】:

    标签: android android-recyclerview android-support-library android-constraintlayout


    【解决方案1】:

    你从来没有给你的 recyclerView 一个垂直约束,所以发生的事情是在运行时你的 recyclerView 刚刚跳到屏幕顶部。

    当我复制你的代码时,我看到了这个错误:

    此视图不受垂直约束:在运行时,除非您添加垂直约束,否则它将跳转到顶部

    一些提示 - 如果您在 XML 视图中看到红线/设计中的红色警告按钮,您应该真正查看错误并相应地更改布局。

    当然,我不会让你没有一个好的工作布局 - 这是我为 recyclerView 制作的布局,从屏幕的 30% 开始,我已经检查了它及其工作。 (我正在使用 androidx,但如果需要,只需将其更改为支持库)

    <androidx.constraintlayout.widget.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="match_parent">
    
    <androidx.constraintlayout.widget.Guideline
        android:id="@+id/guideline2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        app:layout_constraintGuide_percent="0.3"/>
    
    <androidx.recyclerview.widget.RecyclerView
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginBottom="16dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.506"
        app:layout_constraintStart_toStartOf="@+id/guideline2"
        app:layout_constraintTop_toTopOf="parent"
        android:background="@color/colorPrimaryDark"/>
    

    【讨论】:

    • 不适合我我想要recyclerview的高度wrap_content
    • 而当您将高度更改为 wrap_content 时,您看不到您的视图吗?
    • 你的 recyclerView 中有项目吗?或者目前什么都没有?无论如何,请尝试查看 this thread 解释 recyclerVIew 中的包装内容
    【解决方案2】:

    您的RecyclerView 缺少垂直约束;添加至少一个,以便可以呈现视图。

    限制在顶部:

    app:layout_constraintTop_toTopOf="parent"
    

    如果您希望它使用整个父级,也添加这些:

    android:layout_height="0dp"
    app:layout_constraintBottom_toBottomOf="parent"
    

    【讨论】:

    【解决方案3】:

    移除 ConstraintLayout 并改用 Linearlayout,而不是在 guideline 和 RecyclerView 中相应地使用 android:weight= 属性。

    【讨论】:

    • 嘿 Bhavik Parmar,我已经在下面发布了一个带有约束布局的工作示例,请不要取消约束布局的资格 - 这是一个很棒且易于学习的工具,它为您提供支持不同屏幕尺寸的选项.
    【解决方案4】:

    这对你来说很好用

    <android.support.constraint.ConstraintLayout 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.support.v7.widget.RecyclerView
            android:id="@+id/recycler"
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:clipToPadding="false"
            android:orientation="horizontal"
            android:paddingStart="20dp"
            android:paddingEnd="20dp"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintEnd_toEndOf="@id/guideline"
            app:layout_constraintBottom_toBottomOf="parent"
            />
    
        <androidx.constraintlayout.widget.Guideline
            android:id="@+id/guideline"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="vertical"
           app:layout_constraintGuide_percent=".3" />
    
    
    </android.support.constraint.ConstraintLayout>
    

    【讨论】:

    • 已经有2个答案和你的很相似了,难道是你误解了问题?您按照他的要求水平放置了指南,而不是垂直放置。
    猜你喜欢
    • 2020-01-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-11-26
    • 2017-10-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多