如何在运行时为按钮添加边距? [英] How to add margin to a Button at run time?

查看:112
本文介绍了如何在运行时为按钮添加边距?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须在LinearLayout上添加多个按钮.但是我想把那些按钮说成5px.我找不到设置Button的边距的方法.有什么主意吗?

I have to add more than one buttons on a LinearLayout. But I want to put those buttons say 5px apart. I could not find a way to set a margin of a Button. Any idea?

推荐答案

使用按钮元素的layout_margin属性.那行不通吗?

Use the layout_margin property of the button element. Does that not work?

<Button android:layout_margin="5px" (...)/>

编辑

在Java中创建按钮时,LayoutParams用于指定边距,等等.

Edit

When creating a button in java, LayoutParams is used to specify margins and so.

Button button = new Button();
(....)
params = new LinearLayout.LayoutParams();
params.leftMargin = 5;
(set params as you need)
parent.addView(button, params);

您使用的LayoutParams必须与添加按钮的布局匹配(请参见 http://developer.android.com/reference/android/view/ViewGroup.LayoutParams.html ).对于LinearLayout,请使用LinearLayout.LayoutParams并相应地设置* Margin字段.

The LayoutParams you use must match the Layout you add your button in (see http://developer.android.com/reference/android/view/ViewGroup.LayoutParams.html). For a LinearLayout, use a LinearLayout.LayoutParams and set the *Margin fields accordingly.

这篇关于如何在运行时为按钮添加边距?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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