How to remove Android image button margin programmatically

Asked 2 years ago, Updated 2 years ago, 93 views

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

    for (int rowCounter = 0; rowCounter < DIMENSION; rowCounter++)
        for (int columnCounter = 0; columnCounter < DIMENSION; columnCounter++) {

            LinearLayout rl = new LinearLayout(this);
            GridLayout.LayoutParams paramsL = new GridLayout.LayoutParams();

            paramsL.setMargins(0, 0, 0, 0);
            rl.setLayoutParams(paramsL);
            rl.getLayoutParams().height = (int) getResources().getDimension(R.dimen.imageButton_height);
            rl.getLayoutParams().width = (int) getResources().getDimension(R.dimen.imageButton_width);
            ((ViewGroup.MarginLayoutParams)rl.getLayoutParams()).leftMargin = 0;


            WampusState state = new WampusState(rowCounter,columnCounter);
            state.imgbtn = new ImageButton(this);
            state.imgbtn.setImageResource(R.drawable.bar_back);

           state.imgbtn.setScaleType(ImageView.ScaleType.FIT_CENTER);

            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams
                    (ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
            state.imgbtn.setLayoutParams(params);
            params.setMargins(0, 0, 0, 0);

 rl.addView(state.imgbtn);

                gameBoard.addView(rl);
}

Run as follows:

The image button is inserted in the linear layout, and the top, bottom, left, and right margins are generated. Please tell me how to get rid of the margin

android android-layout android-widget

2022-09-21 15:22

1 Answers

This may be the margin (or padding) added by GridLayout. Please refer to the following article to adjust GridLayout settings.


2022-09-21 15:22

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.