Dynamically generated buttons don't occupy even space in GridLayout

不问归期 提交于 2019-12-12 03:51:08

问题


I have a GridLayout in which I want Buttons to be generated dynamically. I have specified the number of rows & columns of the GridLayout in XML. I want the generated buttons to occupy even amount of space in the GridLayout.

That is to say, if I have three columns, all three buttons should occupy same space. Now I have seen a few examples here on StackOverflow that suggest making use of android:layout_width, but none of them contain examples of dynamically generated Views & I don't know if and how layout_width can be added to a View component like Button.

I want my GridLayout to look like this, but all I'm getting is this

Following is the XML that contains GridLayout

<GridLayout
                            android:id="@+id/gridLayout"
                            android:layout_marginTop="30dp"
                            android:layout_gravity="center"
                            android:background="@android:color/holo_blue_light"
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:columnCount="2"
                            android:rowCount="2"
                            android:columnOrderPreserved="true"
                            android:scrollbars="vertical">
                        </GridLayout>

Following is the dynamic generation of Buttons

netbankingGrid = (GridLayout) findViewById(R.id.gridLayout);

        //First Cell on the Grid
        {
            Button button1 = new Button(this);
            button1.setText("Button1");
            Drawable image = getApplicationContext().getResources().getDrawable(R.drawable.ar_hdfc);
            image.setBounds(0,0,70,70);
            button1.setCompoundDrawables(null,image,null, null);
            button1.setBackgroundResource(R.drawable.custom_bg1);
            netbankingGrid.addView(button1);
        }

        //Second Cell on the Grid
        {

            Button button2 = new Button(this);
            button2.setText("Button2");
            Drawable image = getApplicationContext().getResources().getDrawable(R.drawable.ar_hdfc);
            image.setBounds(0,0,70,70);
            button2.setCompoundDrawables(null,image,null, null);
            button2.setBackgroundResource(R.drawable.custom_bg2);
            netbankingGrid.addView(button2);
        }

        //Third Cell on the Grid
        {

            Button button3 = new Button(this);
            button3.setText("Button3");
            Drawable image = getApplicationContext().getResources().getDrawable(R.drawable.ar_hdfc);
            image.setBounds(0,0,70,70);
            button3.setCompoundDrawables(null,image,null, null);
            button3.setBackgroundResource(R.drawable.custom_bg3);
            netbankingGrid.addView(button3);
        }

        //Fourth Cell on the Grid
        {

            Button button4 = new Button(this);
            button4.setText("Button4");
            Drawable image = getApplicationContext().getResources().getDrawable(R.drawable.ar_hdfc);
            image.setBounds(0,0,70,70);
            button4.setCompoundDrawables(null,image,null, null);
            button4.setBackgroundResource(R.drawable.custom_bg4);
            netbankingGrid.addView(button4);
        }

Thank you for your time!

来源:https://stackoverflow.com/questions/38590792/dynamically-generated-buttons-dont-occupy-even-space-in-gridlayout

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!