java - 以编程方式添加时如何为 GridLayout 子项设置 layout_columnWeight?

标签 java android android-gridlayout

我想使用 GridLayout(不是 GridView)作为国际象棋或西洋跳棋等游戏的棋盘。由于我不太愿意使用包含 64 个子 View 的 xml 文件,因此我尝试以编程方式添加它们。

为了简单起见,我开始使用 TextView 作为 GridLayout 的子 View

我的问题是 View 分布不均匀,而且我不知道如何在我的 java 代码中实现均匀分布。没有像“setWeight()”这样的方法来设置 layout_columnWeightlayout_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>

我已将 GridLayout 宽度和高度设置为 match_parent ,但我在运行时使用 ViewTreeObserver.OnPreDrawListener 更改它们为了得到一 block 方板。这有效,彩色背景显示了预期的正方形空间。

我的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;
        }
    });
}

我已经尝试过的:

我在布局文件中放置了一个 TextView 子项,并尝试从其 GridLayout.LayoutParams< 复制 layout_columnWeightlayout_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>

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

看来我没有设法通过使用复制构造函数来设置所需的权重。

最佳答案

-给你! :>

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

关于java - 以编程方式添加时如何为 GridLayout 子项设置 layout_columnWeight?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35600406/

相关文章:

java - JasperReports 服务器 : Getting "java.util.GregorianCalendar not configured" error when using java. util.Calendar 在 scriptlet

android - 是否可以向抽屉导航按钮添加声音?

android - 在 gridLayout 中从右到左放置项目

java - GridLayout 不会显示在网格中

android gridlayout 未在模拟器 API 22 中显示

java - 使用 log4j 设置文件名时使用什么扩展名

java - 硬币方法中的递归方法

java - 如何使用 fragment 将联系人选择到编辑文本中

java - Android 本地主机上的套接字通信

java - 如果应用程序请求权限,如何获取?