【发布时间】:2019-06-18 08:50:37
【问题描述】:
我正在使用 GridLayout,它最初包含 3 个 cardviews。现在,当用户单击最后一张卡片时,我想再向GridLayout 添加一张卡片。
但在我在xml 文件中创建的最初 3 张卡片中包含属性“columnweight”。所以我想将此属性设置为onClick之后新创建的cardview。
我看到了一些与此相关的问题。但是他们也在以编程方式创建gridlayout,并且不想以编程方式创建gridLayout。我该怎么做?
注意:- 我的每个 CradView 结构都是这样的:- Cardview->LinearLayout->TextView
我已经浏览了以下链接 this
但正如我所说,他们以编程方式创建了 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>
【问题讨论】:
标签: android android-layout android-cardview