CardView和Cardview高度内的RecyclerView设置为环绕高度,而不是环绕高度

时间:2018-08-18 11:31:51

标签: android android-studio android-recyclerview android-cardview

RecyclerViewCardviewCardview内的高度设置为环绕高度,而不是环绕高度,表示有更多空间。

enter image description here

XML代码

<android.support.v4.widget.NestedScrollView
                    android:id="@+id/ns1"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:layout_below="@+id/lcat1"
                    android:fillViewport="true"

                    app:layout_behavior="@string/appbar_scrolling_view_behavior">


                    <LinearLayout
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        android:orientation="vertical">

                        <android.support.v7.widget.CardView
                            android:id="@+id/card_background_new_design"
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:layout_margin="5dp"
                            app:cardElevation="5dp">

                            <LinearLayout
                                android:layout_width="match_parent"
                                android:layout_height="wrap_content"
                                android:orientation="vertical">

                                <LinearLayout

                                    android:layout_width="match_parent"
                                    android:layout_height="wrap_content"
                                    android:orientation="horizontal"
                                    android:weightSum="1">

                                    <TextView
                                        android:id="@+id/bestdeal"
                                        android:layout_width="match_parent"
                                        android:layout_height="wrap_content"
                                        android:layout_weight="0.5"
                                        android:padding="5dp"
                                        android:paddingLeft="10dp"
                                        android:text="DEALS"
                                        android:textColor="@color/primary_light"
                                        android:textSize="12sp"
                                        android:textStyle="bold" />

                                    <TextView
                                        android:id="@+id/viewall"
                                        android:layout_width="match_parent"
                                        android:layout_height="wrap_content"
                                        android:layout_gravity="right"
                                        android:layout_weight="0.5"
                                        android:gravity="right"
                                        android:padding="5dp"

                                        android:text="View All"
                                        android:textColor="@color/primary_light"
                                        android:textSize="12sp"

                                        android:textStyle="bold" />
                                </LinearLayout>

                                <android.support.v7.widget.RecyclerView
                                    android:id="@+id/recycler_view_page2"
                                    android:layout_width="match_parent"
                                    android:layout_height="wrap_content"
                                    android:layout_marginBottom="10dp"
                                    android:clipToPadding="false"
                                    android:paddingBottom="10dp"
                                    android:orientation="horizontal"
                                    android:background="@color/redmartredcolor"
                                    android:paddingTop="10dp"
                                    android:scrollbars="none" />
                            </LinearLayout>

                        </android.support.v7.widget.CardView>


                    </LinearLayout>

                </android.support.v4.widget.NestedScrollView>

4 个答案:

答案 0 :(得分:0)

请通过以下属性管理名片视图的内容:

<?=$this->Number->currency($price, 'EUR', ['precision' => 0])?>

我希望从内容中删除多余的空间。

答案 1 :(得分:0)

尝试以下代码。我做了一些更改,它应该可以正常工作。

更新:

<android.support.v4.widget.NestedScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/ns1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fillViewport="true"
    app:layout_behavior="@string/appbar_scrolling_view_behavior">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <android.support.v7.widget.CardView
            android:id="@+id/card_background_new_design"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="5dp"
            app:cardElevation="5dp">

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical">

                <LinearLayout

                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:orientation="horizontal"
                    android:weightSum="1">

                    <TextView
                        android:id="@+id/bestdeal"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_weight="0.5"
                        android:padding="5dp"
                        android:paddingEnd="10dp"
                        android:paddingStart="10dp"
                        android:text="DEALS"
                        android:textColor="@color/colorPrimary"
                        android:textSize="12sp"
                        android:textStyle="bold" />

                    <TextView
                        android:id="@+id/viewall"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_weight="0.5"
                        android:gravity="end"
                        android:padding="5dp"
                        android:text="View All"
                        android:textColor="@color/colorPrimary"
                        android:textSize="12sp"
                        android:textStyle="bold" />

                </LinearLayout>

                <android.support.v7.widget.RecyclerView
                    android:id="@+id/recycler_view_page2"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:layout_gravity="bottom"
                    android:background="@color/red"
                    android:paddingTop="10dp" />

            </LinearLayout>

        </android.support.v7.widget.CardView>


    </LinearLayout>

</android.support.v4.widget.NestedScrollView>

编辑#2:(简单而又新的实现方式)

<android.support.v4.widget.NestedScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/ns1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fillViewport="true"
    app:layout_behavior="@string/appbar_scrolling_view_behavior">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="horizontal">

        <android.support.v7.widget.CardView
            android:id="@+id/card_background_new_design"
            android:layout_width="match_parent"
            android:layout_height="200dp"
            android:layout_margin="5dp"
            app:cardElevation="5dp">

            <RelativeLayout

                android:layout_width="match_parent"
                android:layout_height="wrap_content">

                <TextView
                    android:id="@+id/bestdeal"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:padding="5dp"
                    android:paddingEnd="10dp"
                    android:paddingStart="10dp"
                    android:text="DEALS"
                    android:textColor="@color/colorPrimary"
                    android:textSize="12sp"
                    android:textStyle="bold" />

                <TextView
                    android:id="@+id/viewall"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:gravity="end"
                    android:padding="5dp"
                    android:text="View All"
                    android:textColor="@color/colorPrimary"
                    android:textSize="12sp"
                    android:textStyle="bold" />

                <android.support.v7.widget.RecyclerView
                    android:id="@+id/recycler_view_page2"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="30dp"
                    android:background="@color/red"
                    android:paddingTop="10dp" />

            </RelativeLayout>

        </android.support.v7.widget.CardView>


    </LinearLayout>

</android.support.v4.widget.NestedScrollView>

答案 2 :(得分:0)

最终解决方案是

ViewTreeObserver vto = mRecyclerView.getViewTreeObserver();
        vto.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {

                Log.i("recy_",mRecyclerView.getHeight()+"");
                card_view_background.getLayoutParams().height=mRecyclerView.getHeight()+(mRecyclerView.getHeight()*10/100);
                card_view_background.requestLayout();
            }
        });

答案 3 :(得分:0)

制作

android:layout_height="match_parent"

用于Cardview及其父级LinearLayout。