Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/194.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 在gridlayout中对齐动态按钮_Java_Android - Fatal编程技术网

Java 在gridlayout中对齐动态按钮

Java 在gridlayout中对齐动态按钮,java,android,Java,Android,我正在尝试对齐网格布局中的动态按钮 问题: 但是,如果文本不止一行,则在行中形成按钮 这不是必要的 字符串项[]=新字符串[]{“item1”、“item2”、“item3是”, “第4项”、“第5项”} Button[]myButton=新按钮[item.length] //GridLayout.LayoutParams leftMarginParams=新建GridLayout.LayoutParams( // ); for(int index=0;index

我正在尝试对齐
网格布局中的动态按钮

问题:

但是,如果文本不止一行,则在行中形成按钮 这不是必要的

字符串项[]=新字符串[]{“item1”、“item2”、“item3是”, “第4项”、“第5项”}

Button[]myButton=新按钮[item.length]

//GridLayout.LayoutParams leftMarginParams=新建GridLayout.LayoutParams( // );

for(int index=0;index

`

我建议使用适配器,然后根据需要进行自定义 看看这个例子

我建议使用适配器,然后根据需要进行自定义 看看这个例子

我认为在GridLayoutManager中使用recyclerView应该会对您有所帮助


这篇文章应该能帮到你

我认为在GridLayoutManager中使用recyclerView应该会对您有所帮助


这篇文章应该能帮到你

您只需要应用布局重力,因为默认设置似乎是将文本对齐到相同的高度(这就是为什么只有项目3有两行)


您只需要应用布局重力,因为默认设置似乎是将文本对齐到相同的高度(这就是为什么只有项目3有两行)


为什么不使用适配器?我是android新手,你能告诉我一些例子吗?为什么不使用适配器?我是android新手,你能告诉我一些例子吗?谢谢你的例子,但是当我在android studio复制和粘贴代码时。无法识别“GridAdapter”。有没有办法控制gridlayout的孩子们而不是使用gridview?下载源代码并导入它,而不仅仅是复制和粘贴!Thk u工作正常,但它产生了另一个问题,我需要在单击每个项目时使用getTag或getID函数。但不是从主活动类工作谢谢你的例子,但当我在android studio中复制和粘贴代码时。无法识别“GridAdapter”。有没有办法控制gridlayout的孩子们而不是使用gridview?下载源代码并导入它,而不仅仅是复制和粘贴!Thk u工作正常,但它产生了另一个问题,我需要在单击每个项目时使用getTag或getID函数。但不在主要活动课上工作
GridLayout scrViewButLay = (GridLayout) findViewById(R.id.grid);
    scrViewButLay.setColumnCount(4);
    scrViewButLay.setRowCount(10);
 for (int index = 0; index < item.length ; index++) {


        myButton[index] = new Button(this); //initialize the button here
        myButton[index].setText(item[index]);
        myButton[index].setHeight(100);
        myButton[index].setWidth(100);
        myButton[index].setTag(index);

        scrViewButLay.addView(myButton[index]);

    }
}
GridLayout.LayoutParams param =new GridLayout.LayoutParams();
    param.height = GridLayout.LayoutParams.WRAP_CONTENT;
    param.width = GridLayout.LayoutParams.WRAP_CONTENT;
    param.setGravity(Gravity.BOTTOM);