Change the value of margin and padding with java code

Asked 2 years ago, Updated 2 years ago, 23 views

Hello!

I'm coding for adding buttons dynamically with Java code

If you press the button on the far left in the picture below, the buttons on the right are formed.

However, as shown in the picture, there is a frame gap between the TAB0, TAB1, and TAB2 buttons, and I searched to remove the gap, but it didn't apply, so I'm posting a question. ㅠ<

The new buttons are custom buttons made by extending Button.

In the code in it

 private void init(Context context, String indicator) {
        int width = dpToPx(context, 100);
        int height = dpToPx(context, 48);
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(width, height);
        params.bottomMargin = 0;
        params.leftMargin = 0;
        params.rightMargin = 0;
        params.topMargin = 0;
        this.setLayoutParams(params);
        this.setText(indicator);
        this.setGravity(Gravity.CENTER);
        this.setPadding(0,0,0,0);
    }

I don't know what'ㅠ<

android

2022-09-22 12:50

1 Answers

Try using style to get rid of margin. I think the gap doesn't disappear because there is the default margin on Android.

If you remove the margin from the styles.xml, the margin on the button will disappear. Where you create the button new Button (context, atrs (if you don't have one, you can pass it to null), R.style.MyApp.Button) Declare it like this.

styles.xml
    <style name="MyApp.Button" parent="Base.Widget.AppCompat.Button">
        <item name="android:layout_margin">0dp</item>
    </style>


2022-09-22 12:50

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.