首页 > 解决方案 > cardView 仅显示 2 个项目

问题描述

我有一个包含很多项目的循环视图,但在屏幕上我只能看到 2 个项目,但如果我滚动我可以看到其余的项目,我的想法是在屏幕上看到很多,而不仅仅是 2。

这是我的xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="100dp"
        android:paddingBottom="@dimen/activity_vertical_margin"
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:paddingTop="@dimen/activity_vertical_margin"
        app:layout_behavior="@string/appbar_scrolling_view_behavior">

    <android.support.v7.widget.CardView
            android:id="@+id/card"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            android:layout_margin="16dp"
            app:cardCornerRadius="4dp"
            app:cardElevation="4dp"
            app:cardUseCompatPadding="false" >

        <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="vertical"
                android:gravity="center_horizontal"
                android:layout_marginTop="75dp"
                android:padding="24dp">

            <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:textSize="15dp"
                    android:id="@+id/text"
                    android:textColor="#727272"/>


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


    <ImageView
            android:id="@+id/image"
            android:layout_width="150dp"
            android:layout_height="150dp"
            android:layout_marginTop="75dp"
            android:layout_centerHorizontal="true"
            android:src="@mipmap/ic_launcher"
            android:elevation="8dp" />

</RelativeLayout>

我的目标是这样的 在此处输入图像描述

编辑

这是我当前的输出(在顶部),我的预期输出就像底部一样,事情是降低高度并增加重量

在此处输入图像描述

标签: androidandroid-layoutlayout

解决方案


请检查下面的代码片段以查看我的输出。我正在使用GridLayoutManager并传递 2,它被用作多个列。

MainActivity.java

 MyRecyclerViewAdapter myRecyclerViewAdapter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        String[] data = {"1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23", "24", "25", "26", "27", "28", "29", "30", "31", "32", "33", "34", "35", "36", "37", "38", "39", "40", "41", "42", "43", "44", "45", "46", "47", "48"};

        // set up the RecyclerView
        RecyclerView recyclerView = findViewById(R.id.rvNumbers);
        int numberOfColumns = 2;
        recyclerView.setLayoutManager(new GridLayoutManager(this, numberOfColumns));
        myRecyclerViewAdapter = new MyRecyclerViewAdapter(this, data);
        myRecyclerViewAdapter.setClickListener(this);
        recyclerView.setAdapter(myRecyclerViewAdapter);
    }

recyclerview_item.xml

<?xml version="1.0" encoding="utf-8"?>

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content">
    <ImageView
        android:id="@+id/image"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:layout_centerHorizontal="true"
        android:elevation="8dp"
        android:src="@mipmap/ic_launcher" />
    <android.support.v7.widget.CardView
        android:id="@+id/card"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:layout_marginTop="80dp"
        android:layout_marginLeft="16dp"
        android:layout_marginRight="16dp"
        app:cardCornerRadius="4dp"
        app:cardElevation="4dp"
        app:cardBackgroundColor="#000888"
        app:cardUseCompatPadding="false">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_marginTop="75dp"
            android:gravity="center_horizontal"
            android:orientation="vertical"
            android:padding="24dp">

            <TextView
                android:id="@+id/info_text"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textColor="#727272"
                android:textSize="15dp" />
        </LinearLayout>
    </android.support.v7.widget.CardView>
</RelativeLayout>
</FrameLayout>

输出


推荐阅读