带有recyclerview的cardview,其最大高度应在dp和minheight之间,以包装内容

我有一个CardView和一个RecyclerView

 <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”应为该卡的最大高度。如果RecyclerView的项目较少,则应该为wrap_content

基本上,高度应为包装内容,最大高度为300dp。

请指导我如何实现

它在nestedScrollView内部。
我已经尝试将CardView的高度设置为0dp。
我还有另外3张这样的卡片,握住RecyclerViews

mff123456789 回答:带有recyclerview的cardview,其最大高度应在dp和minheight之间,以包装内容

尝试一下

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>

这应该有效

,

您将卡布局android:layout_height设置为wrap_content,并为CardView android:maxHeight="300dp"的最大高度添加了另一个属性

           <androidx.cardview.widget.CardView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:maxHeight="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>
本文链接:https://www.f2er.com/3168601.html

大家都在问