Java ConstraintLayout:无法包装内容并堆叠RecyclerView项
我有一个RecyclerView,我正在尝试用CardView对象填充它。每个的根视图都是ConstraintLayout 我希望我的Recyclerview中的每张卡都占据垂直屏幕空间的25%,并进行堆叠。我使用了各种准则来设置这些约束 我注意到,虽然我可以查看CardView,但两张卡之间有足够的空间。当我希望CardView彼此堆叠时,RecyclerView似乎正在填充与match_父视图高度相同的视图: 回收视图:Java ConstraintLayout:无法包装内容并堆叠RecyclerView项,java,android,xml,android-recyclerview,android-constraintlayout,Java,Android,Xml,Android Recyclerview,Android Constraintlayout,我有一个RecyclerView,我正在尝试用CardView对象填充它。每个的根视图都是ConstraintLayout 我希望我的Recyclerview中的每张卡都占据垂直屏幕空间的25%,并进行堆叠。我使用了各种准则来设置这些约束 我注意到,虽然我可以查看CardView,但两张卡之间有足够的空间。当我希望CardView彼此堆叠时,RecyclerView似乎正在填充与match_父视图高度相同的视图: 回收视图: <?xml version="1.0" encoding="ut
<?xml version="1.0" encoding="utf-8"?>
每个RecyclerView
项目的高度由约束LAyout
决定。由于ConstraintLayout
的高度为match\u parent
每个项目将占据整个屏幕。在ConstraintLayout
中,CardView
将占25%。其余75%将为空白。这就是为什么你会看到你的报告
因此,您需要一种方法使ConstraintLayout
为屏幕高度的25%。不幸的是,除了通过代码,无法直接为RecyclerView
的孩子指定25%的身高。(这并不完全正确:例如,如果您在100dp
处定义了RecyclerVIew
,您可以将ConstraintLayout
定义为25dp
,然后使用它。我假设您不想在代码中指定确切的高度。)
要达到25%的高度,您需要在布局后确定RecyclerView
的高度,并在onCreateViewHolder()
中为RecyclerView
设置适配器的ConstraintLayout
的高度。我认为ConstraintLayout有一个属性,但这似乎是一个限制。我无法将我的准则用于根约束Layout?@tccpg288 A准则只能由该准则的同级引用。准则无法超出其约束范围来更改布局的大小。明白了,我认为这在某种程度上是约束范围的限制,您同意吗?它基本上没有wrap_内容功能,当我将wrap_内容应用于RecyclerView项的Constraint_布局时,它会最小化我的所有视图。
<android.support.v7.widget.RecyclerView
android:id="@+id/recycler_view_ingredients"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
</android.support.v7.widget.RecyclerView>
<?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="match_parent">
<android.support.constraint.Guideline
android:id="@+id/guideline_zero"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
app:layout_constraintGuide_percent=".025" />
<android.support.constraint.Guideline
android:id="@+id/guideline_one"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
app:layout_constraintGuide_percent="0.975" />
<android.support.constraint.Guideline
android:id="@+id/guideline_two"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
app:layout_constraintGuide_percent="0.025" />
<android.support.constraint.Guideline
android:id="@+id/guideline_four"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
app:layout_constraintGuide_percent="0.25" />
<android.support.v7.widget.CardView
android:id="@+id/recipe_cardview"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintEnd_toStartOf="@+id/guideline_one"
app:layout_constraintStart_toStartOf="@+id/guideline_zero"
app:layout_constraintTop_toTopOf="@+id/guideline_two"
app:layout_constraintBottom_toTopOf="@+id/guideline_four">
<TextView
android:id="@+id/id_of_recipe_item"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center" />
</android.support.v7.widget.CardView>
</android.support.constraint.ConstraintLayout>