Android ConstraintLayout:无法缩放图像以适应RecyclerView中的比率 我与StaggedGridLayoutManager有一个回收者视图 在中,我有自定义项目/视图 每个项目都定义为一个ConstraintLayout,其中有一个假定具有恒定纵横比的图像 但图像仍然能够缩放以适应每个跨度的宽度 然后根据比例调整高度
但不知何故,这些图像并没有保持高宽比 以下是创建回收器视图的xml文件和代码: item.xmlAndroid ConstraintLayout:无法缩放图像以适应RecyclerView中的比率 我与StaggedGridLayoutManager有一个回收者视图 在中,我有自定义项目/视图 每个项目都定义为一个ConstraintLayout,其中有一个假定具有恒定纵横比的图像 但图像仍然能够缩放以适应每个跨度的宽度 然后根据比例调整高度,android,android-recyclerview,aspect-ratio,android-constraintlayout,Android,Android Recyclerview,Aspect Ratio,Android Constraintlayout,但不知何故,这些图像并没有保持高宽比 以下是创建回收器视图的xml文件和代码: item.xml <?xml version="1.0" encoding="utf-8"?> <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:padding="10dp">
<ImageView
android:id="@+id/image"
android:layout_width="0dp"
android:layout_height="0dp"
android:adjustViewBounds="true"
android:scaleType="centerCrop"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintDimensionRatio="16:9"
tools:src="@drawable/ic_logo" />
<TextView
android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintBottom_toBottomOf="@+id/image"
android:textColor="@color/white"
tools:text="SAMPLE"
android:paddingBottom="10dp"
android:layout_margin="5dp"/>
</android.support.constraint.ConstraintLayout>
每个项目的宽度都设置得很好,并且适合每个柱跨度的宽度。
但是高度没有保持,而且很少
有人能帮忙吗
感谢您的图像,您可以指定哪个维度应与约束匹配,同时调整其他维度以满足比率。有关详细信息,请参阅文档中标题为“比率”的部分 如果两个尺寸都设置为匹配_约束(0dp),也可以使用比率。在这种情况下,系统设置满足所有约束的最大尺寸,并保持指定的纵横比。基于另一侧的尺寸约束某一侧的步骤。您可以预先附加W“或H,以分别约束宽度或高度。例如,如果一个标注受两个目标约束(例如,宽度为0dp,以父对象为中心),则可以通过添加字母W(用于约束宽度)或H(用于约束高度)来指示应约束哪一侧比率前面,用逗号分隔:
因此,在您的情况下,我会尝试
app:layout\u constraintDimensionRatio=“H,16:9”
此外,我认为您不需要
android:scaleType=“centerCrop”
或adjustViewBounds
,但您必须试用它才能看到。layout\u constraintDimensionRatio帮助我正确适应高度,谢谢
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="10dp">
<android.support.v7.widget.RecyclerView
android:id="@+id/recycler_view"
android:layout_marginTop="5dp"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentStart="true" />
</RelativeLayout>
adapter = new SampleAdapter(list);
StaggeredGridLayoutManager manager = new StaggeredGridLayoutManager(3, StaggeredGridLayoutManager.VERTICAL);
sampleRecyclerView.setLayoutManager(manager);
sampleRecyclerView.setAdapter(adapter);
<Button android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintDimensionRatio="H,16:9"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintTop_toTopOf="parent"/>