gpt4 book ai didi

android - 约束布局 : Unable to scale image to fit a ratio in RecyclerView

转载 作者:塔克拉玛干 更新时间:2023-11-02 09:01:57 26 4
gpt4 key购买 nike

  • 我有一个带有 StaggeredGridLayoutManager 的回收器 View 。
  • 我有自定义项目/ View 。
  • 每个项目都定义为一个 ConstraintLayout,其中有一个应该具有恒定纵横比的图像。
  • 但图像仍然能够缩放以适应每个跨度的宽度。
  • 然后根据比例调整高度。

但不知何故图像没有保持纵横比。

以下是创建回收器 View 的 xml 文件和代码:

项目.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"
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>

Activity .xml

<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>

生成整个 View 的代码:

adapter = new SampleAdapter(list);
StaggeredGridLayoutManager manager = new StaggeredGridLayoutManager(3, StaggeredGridLayoutManager.VERTICAL);
sampleRecyclerView.setLayoutManager(manager);
sampleRecyclerView.setAdapter(adapter);

每个项目的宽度都设置得很好,适合每个列跨度的宽度。但是高度没有保持并且很少。

有人可以帮忙吗?

谢谢

最佳答案

对于您的图像,您可以指定哪个维度应匹配约束条件,同时调整另一个维度以满足比率。请参阅 Dimensions constraints 的文档中标题为“比率”的部分.

You can also use ratio if both dimensions are set to MATCH_CONSTRAINT (0dp). In this case the system sets the largest dimensions the satisfies all constraints and maintains the aspect ratio specified. To constrain one specific side based on the dimensions of another. You can pre append W," or H, to constrain the width or height respectively. For example, If one dimension is constrained by two targets (e.g. width is 0dp and centered on parent) you can indicate which side should be constrained, by adding the letter W (for constraining the width) or H (for constraining the height) in front of the ratio, separated by a comma:

     <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"/>

因此,在您的情况下,我会尝试 app:layout_constraintDimensionRatio="H,16:9"

此外,我认为您不需要 android:scaleType="centerCrop"adjustViewBounds 但您必须尝试一下才能看到。

关于android - 约束布局 : Unable to scale image to fit a ratio in RecyclerView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43973185/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com