Android 如何以编程方式设置cardView的columnWeight
我最初使用的是Android 如何以编程方式设置cardView的columnWeight,android,android-layout,android-cardview,Android,Android Layout,Android Cardview,我最初使用的是GridLayout,其中包含3个cardwiews。现在,当用户单击最后一张卡时,我想再向GridLayout添加一张卡 但是在我在xml文件中创建的最初3张卡片中包含属性“columnweight”。因此,我想将此属性设置为新创建的cardwiew在onClick之后 我看到了一些与此相关的问题。但是他们也在以编程方式创建gridlayout,不想以编程方式创建gridlayout。我该怎么做 注意:-我的每个CradView结构如下所示:- Cardview->LinearL
GridLayout
,其中包含3个cardwiews
。现在,当用户单击最后一张卡时,我想再向GridLayout
添加一张卡
但是在我在xml
文件中创建的最初3张卡片中包含属性“columnweight
”。因此,我想将此属性设置为新创建的cardwiew
在onClick
之后
我看到了一些与此相关的问题。但是他们也在以编程方式创建gridlayout
,不想以编程方式创建gridlayout
。我该怎么做
注意:-我的每个CradView结构如下所示:-
Cardview->LinearLayout->TextView
我已经浏览了下面的链接
但正如我所说,他们以编程方式创建了gridLayout
,我想这样做
我的初始布局为GridView
包含3个cardwiews
<?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开始支持权重。有关更多详细信息,请参阅。
结束更新
使用GridLayout时有一些限制,以下引用取自
“GridLayout不支持重量原则,因为
定义为重量。通常,不可能
将GridLayout配置为在非平凡空间中分配多余空间
多行或多列之间的比例…用于完全控制
在行或列中过多的空间分布;使用线性布局
用于保存关联单元格组中的组件的子视图。“
下面是一个使用LinearLayout子视图的小示例。(我使用了占据未使用区域的空间视图,并将按钮按到所需位置。)
资料来源:
更新:从API 21开始支持权重。有关更多详细信息,请参阅。
结束更新
使用GridLayout时有一些限制,以下引用取自
“GridLayout不支持重量原则,因为
定义为重量。通常,不可能
将GridLayout配置为在非平凡空间中分配多余空间
多行或多列之间的比例…用于完全控制
在行或列中过多的空间分布;使用线性布局
用于保存关联单元格组中的组件的子视图。“
下面是一个使用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>