简体   繁体   English

如何以编程方式设置cardView的columnWeight

[英]How to set columnWeight of cardView programmatically

I am using GridLayout which contains 3 cardviews initially. 我正在使用最初包含3个cardviews GridLayout Now when user clicks last card I want to add one more card to GridLayout . 现在,当用户单击最后一张卡片时,我想向GridLayout再添加一张卡片。

But in the initial 3 cards which I created in xml file contains attribute ' columnweight '. 但是在我在xml文件中创建的最初的3张卡中,包含属性' columnweight '。 So i want to set this attribute to the newly created cardview after onClick . 所以我想在onClick之后将此属性设置为新创建的cardview

I have seen some question related to this. 我已经看到一些与此有关的问题。 But they are creating their gridlayout also programmatically and don't want to create gridLayout programmatically. 但是他们也在以编程方式创建其gridlayout并且不想以编程方式创建gridLayout How can I do this? 我怎样才能做到这一点?

Note :- My each CradView structure is like this :- Cardview->LinearLayout->TextView 注意:-我的每个CradView结构都是这样的:-Cardview-> LinearLayout-> TextView

I have went through the below link this 我已经通过下面的链接

But as I said they created gridLayout programmatically and I want to do that. 但是正如我所说的,他们gridLayout编程方式创建了gridLayout ,我想这样做。

My initial Layout with GridView containg 3 cardViews is 我最初的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>

Source: GridLayout (not GridView) how to stretch all children evenly 来源: GridLayout(不是GridView)如何均匀拉伸所有子级

UPDATE : Weights are supported as of API 21. See PaulT's answer for more details. 更新 :从API 21开始支持权重。有关更多详细信息,请参见PaulT的答案 END UPDATE There are limitations when using the GridLayout, the following quote is taken from the documentation . 结束更新使用GridLayout时有局限性,以下引用摘自文档

"GridLayout does not provide support for the principle of weight, as defined in weight. In general, it is not therefore possible to configure a GridLayout to distribute excess space in non-trivial proportions between multiple rows or columns ... For complete control over excess space distribution in a row or column; use a LinearLayout subview to hold the components in the associated cell group." “ GridLayout不支持按重量定义的重量原理。通常,因此,无法配置GridLayout来以多余的比例在多行或多列之间分配多余的空间...为了完全控制行或列中多余的空间分布;请使用LinearLayout子视图将组件保留在关联的单元格组中。”

Here is a small example that uses LinearLayout subviews. 这是一个使用LinearLayout子视图的小示例。 (I used Space Views that takes up unused area and pushes the buttons into desired position.) (我使用了“空间视图”,该视图占用了未使用的区域并将按钮推到所需位置。)

<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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM