Android 使用ConstraintLayout将水平recyclerview放置在特定位置
我想将我的horizonatl recyclerview从开始放置在30%屏幕宽度处Android 使用ConstraintLayout将水平recyclerview放置在特定位置,android,android-recyclerview,android-support-library,android-constraintlayout,Android,Android Recyclerview,Android Support Library,Android Constraintlayout,我想将我的horizonatl 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.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没有出现在屏幕上,也不是说你不知道如何实现这一点。你从来没有给你的recyclerview一个垂直约束,所以在运行时,你的recyclerview只是跳到了屏幕的顶部。
复制您的代码时,我看到了以下错误: 此视图没有垂直约束:在运行时,除非添加垂直约束,否则它将跳到顶部 一些提示-如果您在XML中看到视图上的红线/设计中的红色警告按钮,您应该查看错误并相应地更改布局 当然,我不会让你没有一个好的工作布局——这是我为recyclerView做的布局,它从屏幕的30%开始,我已经检查了它和它的工作。(我正在使用androidx,但如果需要,可以将其更改为支持库)
删除约束Layout并改用Linearlayout,而不是在guideline和RecyclerView中相应地使用
android:weight=
属性。您的RecyclerView
缺少垂直约束;至少添加一个,以便可以渲染视图
约束到顶部:
app:layout_constraintTop_toTopOf="parent"
如果希望它使用整个父对象,请添加以下内容:
android:layout_height="0dp"
app:layout_constraintBottom_toBottomOf="parent"
这对你很有用
<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>
Hey Bhavik Parmar,我已经在下面发布了一个约束布局的工作示例,请不要取消约束布局的资格-这是一个很好的学习工具,它为您提供了支持不同屏幕大小的选项。不适合我我希望回收服务的高度包装内容我认为这个答案可能会帮助您更好;不为我工作我想要RecyclereView的高度包裹内容当你将高度更改为包裹内容时,你看不到你的视图吗?你的RecyclereView中有项目吗?还是现在什么都没有?无论如何,试着在RecycleService中解释包装内容,这里已经有两个答案与您的答案非常相似,可能是您误解了这个问题吗?你按他的要求水平放置,而不是垂直放置。
<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>