我有一个带有回收者视图的卡片视图。
<androidx.cardview.widget.CardView
android:layout_width="match_parent"
android:layout_height="300dp"
android:layout_margin="8dp"
app:cardBackgroundColor="@android:color/white"
app:cardCornerRadius="4dp"
app:cardElevation="4dp">
<androidx.recyclerview.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@android:color/white" />
</androidx.cardview.widget.CardView>
提到的高度“ 300dp”应为该卡的最大高度。如果用于“回收者”视图的项目较少,则应为wrap_content。
基本上,高度应为最大高度为300dp的换行内容。
我该如何实现?
位于NestedScrollView中。我已经尝试使用高度0dp的卡片视图。我还有3张这样的卡片,其中有保留回收站视图。
尝试一下
ViewTreeObserver vto = yourRecyclerView.getViewTreeObserver();
vto.addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
public boolean onPreDraw() {
yourRecyclerView.getViewTreeObserver().removeOnPreDrawListener(this);
finalHeight = yourRecyclerView.getMeasuredHeight();
finalWidth = yourRecyclerView.getMeasuredWidth();
Log.d(TAG, "height: " + finalHeight + " width: " + finalWidth);
if (finalHeight < 300) {
yourCardView.setLayoutParams(new CardView.LayoutParams(
CardView.LayoutParams.MATCH_PARENT, finalHeight));
yourCardView.setMinimumHeight(10);
}
return true;
}
});
我希望这可以帮助您!
谢谢。
尝试添加android:minHeight="300dp"
和android:maxheight="wrap_content"
,则您的布局高度应为android:layout_height="wrap_content"
,因此您的代码应如下所示
<androidx.cardview.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="300dp"
android:maxheight="wrap_content"
android:layout_margin="8dp"
app:cardBackgroundColor="@android:color/white"
app:cardCornerRadius="4dp"
app:cardElevation="4dp">
<androidx.recyclerview.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@android:color/white" />
</androidx.cardview.widget.CardView>
这应该工作