线性布局中按钮的边距 [英] Margin of buttons in linear layout

查看:29
本文介绍了线性布局中按钮的边距的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一些按钮并将它们添加到定义为

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>

按钮是用

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);

    }

这些按钮总是有一个我想删除的边距(大约 3 像素).有什么我想念的吗?如果我使用我创建的自定义视图,它们之间没有空格.

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.

我应该设置

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

其中包括小说页边距.在这里您可以找到有关 9-Patch 的更多信息.

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

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

在我看来,如果您想删除边距",您应该为图像创建不同的背景,因为 -3 值不是一个好的解决方法 (IHMO).

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).

这篇关于线性布局中按钮的边距的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆