首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >安卓GridLayout single CardView包揽所有屏幕

安卓GridLayout single CardView包揽所有屏幕
EN

Stack Overflow用户
提问于 2021-01-17 20:21:12
回答 2查看 34关注 0票数 0

我想实现一个2x2的GridLayout,这样我就可以显示4个卡片视图。我的问题是单一的卡片视图占据了所有的空间。当我添加第二个(第0行,第1列)时,它位于屏幕之外(在右侧)。

这是我的布局代码

代码语言:javascript
运行
复制
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
    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="match_parent">

<androidx.gridlayout.widget.GridLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:columnCount="2"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:rowCount="2">

    <androidx.cardview.widget.CardView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_row="0"
        app:layout_column="0" />

    <androidx.cardview.widget.CardView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_row="0"
        app:layout_column="1" />
</androidx.gridlayout.widget.GridLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2021-01-17 20:48:49

两个CardViews都与父对象的宽度匹配,这就是为什么您只看到一个,您可以像这样更改它

代码语言:javascript
运行
复制
android:layout_width="wrap_content"
app:layout_columnWeight="1"

使用app:layout_columnWeight将确保两张卡具有相同的宽度,而不管其内容如何

票数 0
EN

Stack Overflow用户

发布于 2021-01-17 20:37:25

使用两个cardview的layout_width作为wrap_content

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/65760504

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档