网格布局中子项的可见性设置为gone

4
这是我的 .xml 文件。
当我触摸单元格时,可见性设置为 View.GONE,但它只是作为 View.INVISIBLE 消失了。在单元格所在的位置有一个空白的空间。单元格的大小是固定的。
如何配置 GridLayout 以正常工作?
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<GridLayout
    android:id="@+id/grid"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="#DDDDDD"
    android:columnCount="3"
    android:rowCount="3" >

    <LinearLayout
        android:id="@+id/lin1"
        android:layout_width="170px"
        android:layout_height="170px"
        android:background="#0099cc"
        android:onClick="hideCell"
        android:orientation="horizontal" >
    </LinearLayout>

    <LinearLayout
        android:id="@+id/lin2"
        android:layout_width="170px"
        android:layout_height="170px"
        android:background="#99CC00"
        android:onClick="hideCell"
        android:orientation="horizontal" >
    </LinearLayout>

    <LinearLayout
        android:id="@+id/lin3"
        android:layout_width="170px"
        android:layout_height="170px"
        android:background="#FFBB33"
        android:onClick="hideCell"
        android:orientation="horizontal" >
    </LinearLayout>

    <LinearLayout
        android:id="@+id/lin4"
        android:layout_width="170px"
        android:layout_height="170px"
        android:background="#ff4444"
        android:onClick="hideCell"
        android:orientation="horizontal" >
    </LinearLayout>

    <LinearLayout
        android:id="@+id/lin5"
        android:layout_width="170px"
        android:layout_height="170px"
        android:background="#33b5e5"
        android:onClick="hideCell"
        android:orientation="horizontal" >
    </LinearLayout>

    <LinearLayout
        android:id="@+id/lin6"
        android:layout_width="170px"
        android:layout_height="170px"
        android:background="#aa66cc"
        android:onClick="hideCell"
        android:orientation="horizontal" >
    </LinearLayout>

    <LinearLayout
        android:id="@+id/lin7"
        android:layout_width="170px"
        android:layout_height="170px"
        android:background="#9933cc"
        android:onClick="hideCell"
        android:orientation="horizontal" >
    </LinearLayout>

    <LinearLayout
        android:id="@+id/lin8"
        android:layout_width="170px"
        android:layout_height="170px"
        android:background="#669900"
        android:onClick="hideCell"
        android:orientation="horizontal" >
    </LinearLayout>

    <LinearLayout
        android:id="@+id/lin8"
        android:layout_width="170px"
        android:layout_height="170px"
        android:background="#ff8800"
        android:onClick="hideCell"
        android:orientation="horizontal" >
    </LinearLayout>
</GridLayout>

</LinearLayout>

这是 hideCell 方法的代码:
public void hideCell(View v) {
    v.setVisibility(View.GONE);
}
2个回答

2

好的,根据Android GridLayout文档所述:

GONE的解释

在布局方面,GridLayout把可见性状态为GONE的视图视为宽度和高度为零。这与忽略标记为GONE的视图的策略略有不同。例如,如果一个标记为gone的视图单独位于一列中,那么只有在没有定义视图的情况下,该列本身才会折叠为零宽度。如果定义了gravity,则gone视图对布局没有影响,容器应该按照视图从未添加到其中一样进行布局。这些语句同样适用于行和列以及行或列组。

因此,我通过创建自己的GridView类来解决这个问题。


2
你可以在这里发布你的自定义GridView吗?我也在寻找解决方案。 - tainy
1
是的,但这意味着对于GridLayout而言,GONE在视觉上的行为与INVISIBLE相同...这是没有用的,我想不出任何理由使这种行为成为默认行为。 - milosmns
这里有一个可用的自定义小部件,https://dev59.com/FFsX5IYBdhLWcg3wY-gh#65402196 - Utsav Barnwal

-1

例子:

package ua.vsgroup.widgets;

import android.content.Context;
import android.support.v7.widget.GridLayout;
import android.util.AttributeSet;
import android.view.View;

public class vsGridLayout extends GridLayout {

    View[] mChild = null;

    public vsGridLayout(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    public vsGridLayout(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public vsGridLayout(Context context) {
        this(context, null);
    }

    private void arrangeElements() {

        mChild = new View[getChildCount()];
        for (int i = 0; i < getChildCount(); i++) {
            mChild[i] = getChildAt(i);
        }

        removeAllViews();
        for (int i = 0; i < mChild.length; i++) {
            if (mChild[i].getVisibility() != GONE)
                addView(mChild[i]);
        }
        for (int i = 0; i < mChild.length; i++) {
            if (mChild[i].getVisibility() == GONE)
                addView(mChild[i]);
        }

    }

    @Override
    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {

        arrangeElements();
        super.onLayout(changed, left, top, right, bottom);

    }


}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接