android - 如何以编程方式设置cardView的columnWeight
问题描述
我使用的最初GridLayout
包含 3 个。cardviews
现在,当用户单击最后一张卡片时,我想再添加一张卡片GridLayout
。
但在我在xml
文件中创建的最初 3 张卡片中包含属性“ columnweight
”。所以我想将此属性设置为新创建的cardview
after onClick
。
我看到了一些与此相关的问题。但是他们gridlayout
也在以编程方式创建它们,并且不想以gridLayout
编程方式创建。我怎样才能做到这一点?
注意:- 我的每个 CradView 结构都是这样的:- Cardview->LinearLayout->TextView
我已经通过下面的链接 这个
但正如我所说,他们gridLayout
以编程方式创建,我想这样做。
GridView
我包含 3的初始布局cardViews
是
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true">
<LinearLayout android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:id="@+id/activity_manager"
android:weightSum="10">
<Space
android:layout_height="30dp"
android:layout_width="wrap_content"/>
<GridLayout
android:id="@+id/mainGrid"
android:columnCount="2"
android:alignmentMode="alignMargins"
android:columnOrderPreserved="false"
android:layout_weight="5"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="14dp" >
<android.support.v7.widget.CardView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_columnWeight="1"
android:layout_rowWeight="1"
app:cardElevation="8dp"
android:layout_marginRight="16dp"
android:layout_marginLeft="16dp"
android:layout_marginBottom="16dp"
app:cardCornerRadius="8dp"
android:padding="0dp">
<LinearLayout
android:layout_gravity="center_horizontal|center_vertical"
android:layout_margin="4dp"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:text="Mumbai Hackathon"
android:textAlignment="center"
android:layout_marginTop="6dp"
android:textColor="@color/colorPrimary"
android:textSize="18sp"
android:textStyle="bold"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
</android.support.v7.widget.CardView>
<android.support.v7.widget.CardView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_columnWeight="1"
android:layout_rowWeight="1"
app:cardElevation="8dp"
android:layout_marginRight="16dp"
android:layout_marginLeft="16dp"
android:layout_marginBottom="16dp"
app:cardCornerRadius="8dp"
android:padding="0dp">
<LinearLayout
android:layout_gravity="center_horizontal|center_vertical"
android:layout_margin="4dp"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:text="Game of Codes (GOC)"
android:layout_marginTop="6dp"
android:textAlignment="center"
android:textColor="@color/colorPrimary"
android:textSize="18sp"
android:textStyle="bold"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
</android.support.v7.widget.CardView>
<android.support.v7.widget.CardView
android:id="@+id/add_file"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_columnWeight="1"
android:layout_rowWeight="1"
app:cardElevation="8dp"
android:layout_marginRight="16dp"
android:layout_marginLeft="16dp"
android:layout_marginBottom="16dp"
app:cardCornerRadius="8dp">
<LinearLayout
android:layout_gravity="center_horizontal|center_vertical"
android:layout_margin="4dp"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:text="+\nAdd New File"
android:layout_marginTop="6dp"
android:textAlignment="center"
android:textColor="@color/colorPrimary"
android:textSize="18sp"
android:textStyle="bold"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
</android.support.v7.widget.CardView>
</GridLayout>
</LinearLayout>
</ScrollView>
解决方案
更新:从 API 21 开始支持权重。有关详细信息,请参阅PaulT 的答案。 END UPDATE 使用 GridLayout 时存在限制,以下引用来自文档。
“GridLayout 不提供对权重原则的支持,如权重中定义的那样。通常,因此无法配置 GridLayout 以在多行或多列之间以非平凡的比例分配多余的空间......为了完全控制行或列中的多余空间分布;使用 LinearLayout 子视图将组件保存在关联的单元格组中。”
这是一个使用 LinearLayout 子视图的小示例。(我使用了占用未使用区域并将按钮推到所需位置的空间视图。)
<GridLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:columnCount="1"
>
<TextView
android:text="2x2 button grid"
android:textSize="32dip"
android:layout_gravity="center_horizontal" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" android:orientation="horizontal">
<Space
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button 1" />
<Space
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="start"
android:text="Button 2" />
<Space
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<Space
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button 3" />
<Space
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="start"
android:text="Button 4" />
<Space
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1" />
</LinearLayout>
</GridLayout>
推荐阅读
- vert.x - Vertx 应用程序的混沌测试
- python - 哪个是谷歌云中训练有素的机器学习模型的最佳选择?
- php - 我没有在准备好的语句 mysql 中使用,我是 OOP php daskite 的新手
- angular - Angular 2+路由器导航问题
- python - OSError: [Errno 8] Exec format error: 'chromedriver' using Chromedriver on Ubuntu server
- debugging - x86 程序收到信号 SIGSEGV,调试 MASM32 程序集期间出现分段错误
- simulation - 格子玻尔兹曼模拟中消失的内部边界条件
- ios - 我想将特定字符串转换为 json
- javascript - 使用 vform 包在 laravel 中使用 vue 的未定义变量
- amazon-web-services - AWS-sdk 没有用于 rds 集群的分页器