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

0X0*_*gar 7 java android android-gridlayout

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

为了简单起见,我开始使用TextViews作为孩子ViewGridLayout.

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

目前,这是我的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>
Run Code Online (Sandbox Code Playgroud)

我已经将GridLayout宽度和高度设置为match_parent这里,但我在运行时使用a 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,1, 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;
        }
    });
}
Run Code Online (Sandbox Code Playgroud)

我已经尝试过了:

我把一个TextView孩子在布局文件,并试图复制layout_columnWeightlayout_rowWeight从其GridLayout.LayoutParams:

 <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>
Run Code Online (Sandbox Code Playgroud)

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

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

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

 GridLayout.LayoutParams myGLP = new GridLayout.LayoutParams();
Run Code Online (Sandbox Code Playgroud)

 GridLayout.LayoutParams myGLP = new GridLayout.LayoutParams(gridLayoutParamsToCopy);
Run Code Online (Sandbox Code Playgroud)

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

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

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

Ngu*_*Hoà 5

-这个给你 !:>

Button button = new Button(this);
GridLayout.LayoutParams param= new GridLayout.LayoutParams(GridLayout.spec(
            GridLayout.UNDEFINED,GridLayout.FILL,1f),
            GridLayout.spec(GridLayout.UNDEFINED,GridLayout.FILL,1f));
param.height = 0;                                                                                                           
param.width  = 0;
button.setLayoutParams(param);
Run Code Online (Sandbox Code Playgroud)


Luk*_*rog 1

要设置 Gridlayout 子级的权重,请使用采用浮点值的 spec() 方法之一(如这个)

另一种方法是创建一个自定义视图(在这种情况下,您需要手动绘制片段)或自定义 ViewGroup(在这种情况下,自定义 ViewGroup 将只负责片段定位,如果您计划拥有比简单 TextView 更复杂的视图层次结构)。