cardView仅显示2个项目

时间:2018-10-24 23:57:26

标签: android android-layout layout

我有很多项目,但是在屏幕上我只能看到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>

我的目标是这样的 enter image description here

编辑

这是我当前的输出(在顶部),而我的预期输出就像在底部,这是降低身高并增加体重

enter image description here

1 个答案:

答案 0 :(得分:0)

请检查下面的代码片段以查看我的输出。我正在使用 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>

输出