更改GridLayout中卡片视图的方向时如何设置layout_columnWeight =“ 1”

时间:2018-10-11 22:12:20

标签: java android android-layout android-cardview android-layoutparams

在更改方向时尝试减少GridLayout内的列数时遇到了一个问题,我已经使这种方法变通了:

private void changeColumnCount(int columnCount) {
    if (grid.getColumnCount() != columnCount) {
        final int viewsCount = grid.getChildCount();
        for (int i = 0; i < viewsCount; i++) {
            CardView view = (CardView) grid.getChildAt(i);
            view.setLayoutParams(new GridLayout.LayoutParams());
            ViewGroup.MarginLayoutParams cardsMargins = (ViewGroup.MarginLayoutParams) view.getLayoutParams();
            int px = (int)DpToPixel(16,getApplicationContext());
            cardsMargins.setMargins(px,0,px,px);
        }
        grid.setColumnCount(columnCount);
    }
}

我的布局如下:如前所述,我有一个GridLayout,其中包含CardViewsandroid.support.v7.widget.CardView

<GridLayout
        android:id="@+id/mainGrid"
        android:columnCount="2"
        android:rowCount="3"
        android:alignmentMode="alignMargins"
        android:columnOrderPreserved="false"
        android:layout_weight="8"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:padding="14dp">


        <android.support.v7.widget.CardView
            android:layout_width="0dp"
            android:layout_height="120dp"
            android:layout_columnWeight="1"
            android:layout_marginBottom="16dp"
            android:layout_marginLeft="16dp"
            android:layout_marginRight="16dp"
            android:layout_rowWeight="1"
            android:clickable="true"
            app:cardBackgroundColor="@color/cards_color"
            app:cardCornerRadius="8dp"
            app:cardElevation="4dp">

            <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center_horizontal|center_vertical"
                android:layout_margin="16dp"
                android:orientation="vertical">

                <ImageView
                    android:layout_width="70dp"
                    android:layout_height="70dp"
                    android:layout_gravity="center_horizontal"
                    app:srcCompat="@drawable/ic_baseline_info_white" />

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center"
                    android:text="Me"
                    android:textAlignment="center"
                    android:textColor="@android:color/background_light"
                    android:textSize="18sp"
                    android:textStyle="bold" />

            </LinearLayout>
      </android.support.v7.widget.CardView>
              ....
              ....
</GridLayout>

问题是当我调用上述方法时,它会创建一个没有参数android:layout_rowWeight="1"android:layout_columnWeight="1"的新卡片视图

开始就是这样:

Img1

这就是调用该方法后的样子:

Img2

如何更改该方法以使cardViews行填充gridLayout的宽度?

在此先感谢

0 个答案:

没有答案