android - 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>
编辑
这是我当前的输出(在顶部),我的预期输出就像底部一样,事情是降低高度并增加重量
解决方案
请检查下面的代码片段以查看我的输出。我正在使用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);
}
<?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>
输出
推荐阅读
- java - 如何在每个屏幕上禁用 android 后退按钮?
- flutter - 去除颤动的TextField标签文本背景颜色
- reason - 如何在 Reason React 中使用模数运算符?
- python - 为什么从 flow_from_directory 张量流中得到一个空序列?
- powershell - 在 Powershell 中加入两个数据表?
- python - 在每次迭代中,我都在“重新定义/重新分配”或恢复一个变量,这会占用额外的空间吗?
- php - Ngnix + Apache/php-frm 502 网关错误
- android - 我想使用 highchart 在 android 的 stackbar 图表中显示 0% 的数据而没有一列
- ios - 关闭顶层 VC 时不调用 ViewWillAppear
- java - 2.2.6 中缺少 HfileOutputFormat2.java,当我将 hbase lib 从 0.98.6 升级到 2.2.6 时,这个类的替代方案是什么?