【问题标题】:Cardview half width of the screenCardview 屏幕的半宽
【发布时间】:2015-08-06 10:54:42
【问题描述】:

这是我的布局。我正在放入一个 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>

【问题讨论】:

  • 设置numColums为2 GrridView自动制作两列\

标签: android android-cardview


【解决方案1】:

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

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

android:numColumns="2"

或者来自java代码。

mGridView.setNumColumns(2);

【讨论】:

    【解决方案2】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-11-29
      • 1970-01-01
      • 1970-01-01
      • 2021-10-23
      • 2012-03-17
      • 2020-12-19
      • 1970-01-01
      相关资源
      最近更新 更多