简体   繁体   English

线性布局中的按钮边距

[英]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. 这些按钮总是有一个边距(约3px),我想删除。 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); lp.setMargins(-3,-3,-3,-3);

which removes the margin? 除去保证金? Is there a drawback with this? 这有缺点吗?

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 http://developer.android.com/guide/developing/tools/draw9patch.html

which includes fiction margins. 其中包括小说边缘。 Here you can find more info about 9-Patch. 在这里您可以找到有关9-Patch的更多信息。

http://developer.android.com/guide/topics/resources/drawable-resource.html 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). 在我看来,如果你想删除“边距”,你应该为图像创建一个不同的背景,因为-3值不是一个好的解决方法(IHMO)。

Why are you using Layout Params .. simply add the view after its creation... This will surely remove the problem 你为什么使用Layout Params ..只需在创建后添加视图......这肯定会消除问题

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

{
    Button btn = new Button(activity);


    btn.setText("Button");


    mylayout.addView(pv);

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM