Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Margin of buttons in linear layout

I am creating some buttons and add them to a linear layout which is defined as

<LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:id="@+id/mylayout">

    </LinearLayout>

The buttons are created with

for (int i = 0; i < 3; i++)

    {
        Button btn = new Button(activity);
        LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.WRAP_CONTENT,
                LinearLayout.LayoutParams.WRAP_CONTENT);
        lp.setMargins(1, 1, 1, 1);

        btn.setText("Button");
        btn.setPadding(0, 0, 0, 0);

        mylayout.addView(pv, lp);

    }

These buttons always have a margin (about 3px) which I'd like to remove. Is there anything I am missing? If I use a custom view which I created there is no space between them.

Should I set

lp.setMargins(-3, -3, -3, -3);

which removes the margin? Is there a drawback with this?

like image 600
Philipp Avatar asked Nov 27 '25 20:11

Philipp


1 Answers

I do not really think they have a margin but it is related to the background of the button. Probably the default background of the button has a image like this one:

http://developer.android.com/guide/developing/tools/draw9patch.html

which includes fiction margins. Here you can find more info about 9-Patch.

http://developer.android.com/guide/topics/resources/drawable-resource.html

In my opinion if you want to remove the "margins", you should create a different background for the image because the -3 value is not a good workaround (IHMO).

like image 175
Urizev Avatar answered Nov 30 '25 10:11

Urizev



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!