Cardview 屏幕半宽

Cardview half width of the screen

这是我的布局。我正在放入一个 GridView。我想要 GridView 中的两列填充屏幕的宽度。无法使 CardView 布局填满屏幕的一半。我按照这个 link 但没有结果。

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:layout_width="150dp" android:layout_height="wrap_content"
android:gravity="center"
card_view:cardCornerRadius="2dp"
card_view:cardElevation="2sp"
card_view:cardUseCompatPadding="true"
card_view:contentPadding="5dp"
>
<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    >
    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:id="@+id/quote"
        android:text="jalkdjlajflkdajf akjfdlajfljal alfjk"
        android:layout_margin="3dp"
        />
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="5dp"
        android:layout_marginLeft="2dp"
        android:layout_below="@+id/quote"
        >
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/time"
            android:layout_alignParentLeft="true"
            android:text="time started"
            android:layout_centerVertical="true"
            />
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/button1"
            android:layout_alignParentRight="true"
            android:text="5"
            android:layout_marginLeft="3dp"
            />
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/button2"
            android:layout_toLeftOf="@+id/button1"
            android:text="5"
            android:layout_marginLeft="3dp"
            />
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/button3"
            android:layout_toLeftOf="@+id/button2"
            android:text="5"
            android:layout_marginLeft="3dp"
            />
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/button4"
            android:layout_toLeftOf="@+id/button3"
            android:text="5"
            android:layout_marginLeft="3dp"
            />
    </RelativeLayout>
</RelativeLayout>

因为您正在为 GridView 扩充不同的布局文件。您的布局将呈现为全屏宽度视图。但是,当您定义列数或对 GridView 说跨度时,它将有两个完全相同的并排视图,这与您想要实现的完全相同。

所以,只需在 xml 内的 GridView 标签中添加

android:numColumns="2"

或来自 java 代码。

mGridView.setNumColumns(2);

只需将卡片视图的宽度设置为“匹配父级”,网格布局管理器将处理其余部分