Android 约束布局中的动态视图
我有一个以CardView为项目的RecyclerView 下面是我主要活动的布局Android 约束布局中的动态视图,android,android-constraintlayout,Android,Android Constraintlayout,我有一个以CardView为项目的RecyclerView 下面是我主要活动的布局 <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.c
<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"
tools:context="com.example.personal.newz.ui.MainActivity">
<android.support.v7.widget.RecyclerView
android:id="@+id/rv"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintLeft_toLeftOf="parent"></android.support.v7.widget.RecyclerView>
</android.support.constraint.ConstraintLayout>
下面是“我的回收者”视图项目的布局
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="200dp"
android:layout_margin="16dp"
card_view:cardCornerRadius="4dp"
>
<android.support.constraint.ConstraintLayout xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.constraint.Guideline
android:id="@+id/vertical_guideline"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintGuide_percent="0.6" />
<TextView
android:id="@+id/date_tv"
android:layout_width="wrap_content"
android:layout_height="16dp"
android:layout_marginLeft="8dp"
android:layout_marginTop="16dp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:text="2 hours ago" />
<TextView
android:id="@+id/source_tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="8dp"
android:textStyle="bold"
app:layout_constraintLeft_toRightOf="@id/date_tv"
app:layout_constraintTop_toTopOf="@id/date_tv"
tools:text="BBC News" />
<TextView
android:id="@+id/headline_tv"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginLeft="8dp"
android:layout_marginTop="32dp"
android:maxLines="2"
android:textStyle="bold"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toLeftOf="@id/vertical_guideline"
app:layout_constraintTop_toBottomOf="@id/date_tv"
tools:text="Apple admits slowing down older iphones" />
<TextView
android:id="@+id/description_tv"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:maxLines="4"
app:layout_constraintLeft_toLeftOf="@id/date_tv"
app:layout_constraintRight_toLeftOf="@id/vertical_guideline"
app:layout_constraintTop_toBottomOf="@id/headline_tv"
tools:text="Customers have long suspected iPhones slow down over time. Now, Apple has confirmed some models do." />
<ImageView
android:id="@+id/article_image"
android:layout_width="100dp"
android:layout_height="100dp"
android:scaleType="fitXY"
app:layout_constraintHorizontal_bias=".7"
app:layout_constraintLeft_toRightOf="@id/vertical_guideline"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@id/source_tv"
tools:src="@drawable/placeholder" />
<ImageView
android:id="@+id/bookmark_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:src="@drawable/bookmark"
app:layout_constraintEnd_toStartOf="@+id/share_image"
app:layout_constraintStart_toEndOf="@+id/vertical_guideline"
app:layout_constraintTop_toBottomOf="@id/article_image"
app:layout_constraintHorizontal_chainStyle="packed"/>
<ImageView
android:id="@+id/share_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/share_icon"
android:layout_marginTop="8dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@+id/bookmark_image"
app:layout_constraintTop_toBottomOf="@id/article_image" />
</android.support.constraint.ConstraintLayout>
</android.support.v7.widget.CardView>
现在,当我第一次启动我的应用程序时,单个cardview的宽度与父级不匹配,并且有点像它们的宽度是包裹内容。几秒钟后,所有CardView的宽度将自行调整以匹配父级
ConstraintLayout实际上不支持为其子项匹配父项。如果您尝试使用match_parent,有时会起作用,有时则不会。Android Studio对match_parent也有点奇怪,有时会允许,有时会自动替换为与上次运行的模拟器匹配的硬编码值。无论如何,不要对ConstraintLayout的子级使用match_parent
使用以下命令代替android:layout\u width=match\u parent:
android:layout_width="0dp"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
android:layout_height="0dp"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
使用以下命令代替android:layout\u height=match\u parent:
android:layout_width="0dp"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
android:layout_height="0dp"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
当我对您的RecyclerView标签进行这些更改时,问题就消失了。谢谢@Ben。我尝试了你的建议,它对我也起了作用。那么,您是否也建议我们永远不要将“匹配父对象”用于约束布局的子对象?我是这样认为的。此页面上匹配父项的Ctrl+f:找到了。再次感谢