我有一个项目布局,在这里我显示一个图像,产品名称,和产品图像。我必须使用约束布局以1:1.5的比例显示图像。但当我加载一个小图像,下面的文字不显示。
以下是我的XML项代码:-
<?xml version="1.0" encoding="utf-8"?>
<layout 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">
<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/coordinatorLayoutCartRoot"
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.jackandphantom.circularimageview.RoundedImage
android:id="@+id/imageViewSlider"
android:layout_width="match_parent"
android:layout_height="0dp"
android:scaleType="centerCrop"
app:layout_constraintBottom_toTopOf="@id/tvTitle"
app:layout_constraintDimensionRatio="WH,1:1.4"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:rounded_radius="0"
tools:src="@tools:sample/avatars" />
<TextView
android:id="@+id/tvTitle"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:gravity="center"
android:padding="4dp"
android:text="Fitted crew neck sweater"
android:textColor="@color/colorBlack"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/imageViewSlider" />
<TextView
android:id="@+id/tvPrice"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:gravity="center"
android:padding="4dp"
android:text="$34.00"
android:textColor="@color/colorBlack"
android:textStyle="bold"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/tvTitle" />
</androidx.constraintlayout.widget.ConstraintLayout>
</layout>
长图像的https://i.imgur.com/QVnljX6.png
如果我用match_parent替换wrap_content,应用程序崩溃时会出现以下错误:-
java.lang.IllegalStateException:页面必须填充整个ViewPager2 (使用match_parent)
发布于 2019-10-21 09:29:51
我发现问题出在膨胀的视图架上。
我有同样的问题,并解决了它的变化
ViewBinding.inflate(inflater)
至
ViewBinding.inflate(inflater, parent, false)
发布于 2019-10-15 17:11:36
在努力解决这个问题的时候,我想出了问题的所在。我的用例是我在使用androidx.viewpager2.widget.ViewPager2,并在RecyclerView中调用普通视图。
如果您仔细注意到错误,您将看到如下所示:
java.lang.IllegalStateException: Pages must fill the whole ViewPager2 (use match_parent)
at androidx.viewpager2.widget.ViewPager2$2.onChildViewAttachedToWindow(ViewPager2.java:170)
二线是解决主要问题的关键。如果打开ViewPager2.java,就会看到
private RecyclerView.OnChildAttachStateChangeListener enforceChildFillListener() {
return new RecyclerView.OnChildAttachStateChangeListener() {
@Override
public void onChildViewAttachedToWindow(@NonNull View view) {
RecyclerView.LayoutParams layoutParams =
(RecyclerView.LayoutParams) view.getLayoutParams();
if (layoutParams.width != LayoutParams.MATCH_PARENT
|| layoutParams.height != LayoutParams.MATCH_PARENT) {
throw new IllegalStateException(
"Pages must fill the whole ViewPager2 (use match_parent)");
}
}
@Override
public void onChildViewDetachedFromWindow(@NonNull View view) {
// nothing
}
};
}
Android并没有使用在xml中分配的match_parent来附加视图。将来可能会在ViewPager2的下一个版本中进行改进。
无论如何,现在要修复它,请将布局参数设置为MATCH_PARENT解释。
view.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
此视图是持有视图寻呼机子程序的父视图。
在你的例子中,它将是androidx.constraintlayout.widget.ConstraintLayout.
view.setLayoutParams(new ConstraintLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
发布于 2020-06-14 22:18:52
在ViewPager2 match_parent的适配器项中设置宽度和高度
https://stackoverflow.com/questions/58341487
复制相似问题