java – 如何以编程方式添加GridLayout子项的layout_columnWeight?

我想使用GridLayout(不是GridView)作为象棋或棋子等游戏的棋盘.由于我有点不愿意使用带有64个子视图的xml文件,我尝试以编程方式添加它们.

为了简单起见,我开始使用TextViews作为GridLayout的子视图.

我的问题是视图不是均匀分布的,我不知道如何在我的java代码中获得均匀分布.设置layout_columnWeight和layout_rowWeight没有类似“setWeight()”的方法.

目前,这是我的activity_dynamic_grid_layout.xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_width="match_parent"
            android:layout_height="match_parent">

<ImageView
    android:layout_width="match_parent"
    android:layout_height="80dp"
    android:id="@+id/ivLogo"
    android:background="#ff0000"
    android:layout_alignParentTop="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"/>

<android.support.v7.widget.GridLayout
    android:id="@+id/grid_layout"
    android:background="#004080"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_below="@+id/ivLogo"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:layout_marginTop="36dp">

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

我在这里将GridLayout的宽度和高度设置为match_parent,但我在运行时使用ViewTreeObserver.OnPreDrawListener更改它们以获得方板.这样,彩色背景显示正方形空间.

我的onCreate()

@Override
protected void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_dynamic_grid_layout);

    GridLayout gl = (GridLayout) findViewById(R.id.grid_layout);
    gl.setColumnCount(8);
    gl.setRowCount(8);

    for(int i=0; i<gl.getRowCount(); i++)
    {
        GridLayout.Spec rowSpec = GridLayout.spec(i,1,GridLayout.FILL);

        for(int j=0;j<gl.getColumnCount();j++)
        {
            GridLayout.Spec colSpec = GridLayout.spec(j,GridLayout.FILL);

            TextView tvChild = new TextView(this);
            tvChild.setText("[ " + i + " | " + j + " ]");
            tvChild.setTextSize(18f);
            tvChild.setTextColor(Color.WHITE);
            tvChild.setGravity(Gravity.CENTER);

            GridLayout.LayoutParams myGLP = new GridLayout.LayoutParams();
            myGLP.rowSpec = rowSpec;
            myGLP.columnSpec = colSpec;

            gl.addView(tvChild,myGLP );
        }
    }

    final View rootView = findViewById(R.id.dynamic_root);

    rootView.getViewTreeObserver().addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener()
    {
        @Override
        public boolean onPreDraw()
        {
            int w = rootView.getMeasuredWidth();
            int h = rootView.getMeasuredHeight();
            int min = Math.min(w,h);

            ViewGroup.LayoutParams lp = gl.getLayoutParams();
            lp.width = min - min % 9;
            lp.height = lp.width;
            gl.setLayoutParams(lp);

            rootView.getViewTreeObserver().removeOnPreDrawListener(this);

            return true;
        }
    });
}

我已经尝试过了:

我将一个TextView子项放在布局文件中,并尝试从其GridLayout.LayoutParams中复制layout_columnWeight和layout_rowWeight:

<android.support.v7.widget.GridLayout
 ...>   
    <TextView
        android:id="@+id/clone_my_params"
        android:text="[ 0 | 0 ]"
        android:textColor="#ffffff"
        android:textSize="18sp"
        app:layout_column="0"
        app:layout_row="0"
        app:layout_columnWeight="1"
        app:layout_rowWeight="1"
    />
</android.support.v7.widget.GridLayout>

在double循环之前onCreate()中的附加代码:

TextView v = (TextView)gl.findViewById(R.id.clone_my_params);
v.setGravity(Gravity.CENTER);
GridLayout.LayoutParams gridLayoutParamsToCopy = new GridLayout.LayoutParams(v.getLayoutParams());

在循环内部,我跳过(i,j)=(0,0)并更改

GridLayout.LayoutParams myGLP = new GridLayout.LayoutParams();

GridLayout.LayoutParams myGLP = new GridLayout.LayoutParams(gridLayoutParamsToCopy);

在更改之前,所有元素都在左上角,多余的空间被赋予最后一行/列.更改后,第一行/列具有多余空间,其他元素没有变化.

双循环后调用gl.invalidate()和/或gl.requestLayout()无效.

所以我似乎没有设法通过使用复制构造函数来设置所需的权重.

解决方法

要在Gridlayout的子项上设置权重,请使用其中一个采用浮点值的spec()(如此 one)方法.

另一种方法是制作自定义视图(在这种情况下,您需要手动绘制碎片)或自定义ViewGroup(在这种情况下,自定义ViewGroup将只处理碎片定位,这将是合适的,如果你计划比简单的TextView具有更复杂的视图层次结构.

相关文章

摘要: 原创出处 https://www.bysocket.com 「公众号:泥瓦匠...
摘要: 原创出处 https://www.bysocket.com 「公众号:泥瓦匠...
今天犯了个错:“接口变动,伤筋动骨,除非你确定只有你一个...
Writer :BYSocket(泥沙砖瓦浆木匠)微 博:BYSocket豆 瓣:...
本文目录 线程与多线程 线程的运行与创建 线程的状态 1 线程...