如何在运行时为按钮添加边距?

发布于 2024-11-30 02:07:41 字数 70 浏览 1 评论 0原文

我必须在 LinearLayout 上添加多个按钮。但我想把这些按钮分开 5 像素。我找不到设置按钮边距的方法。有什么想法吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

电影里的梦 2024-12-07 02:07:41

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

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

编辑

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

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 字段。

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

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

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

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.

沐歌 2024-12-07 02:07:41

试试这个:

//Assuming your button is in a LinearLayout as stated
LinearLayout.LayoutParams params = myButton.getLayoutParams();
params.setMargins(0, 0, 0, 0) //left, top, right, bottom
myButton.setLayoutParams(params);

Try this:

//Assuming your button is in a LinearLayout as stated
LinearLayout.LayoutParams params = myButton.getLayoutParams();
params.setMargins(0, 0, 0, 0) //left, top, right, bottom
myButton.setLayoutParams(params);
潇烟暮雨 2024-12-07 02:07:41
MarginLayoutParams params = (MarginLayoutParams) vector8.getLayoutParams();
params.width = 200; params.leftMargin = 100; params.topMargin = 200;
vector8.setLayoutParams(params);

需要使用类型:MarginLayoutParams

MarginLayoutParams params = (MarginLayoutParams) vector8.getLayoutParams();
params.width = 200; params.leftMargin = 100; params.topMargin = 200;
vector8.setLayoutParams(params);

Need use type of: MarginLayoutParams

风流物 2024-12-07 02:07:41

在按钮上添加填充也可以解决您的问题。看看这个
http://developer.android.com/resources/tutorials/views /hello-formstuff.html#CustomButton

Adding padding to the buttons would also solve your problem. Check this out
http://developer.android.com/resources/tutorials/views/hello-formstuff.html#CustomButton

慕烟庭风 2024-12-07 02:07:41

使用此代码在运行时设置边距

  LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) btnContainer.getLayoutParams();
        params.setMargins(5, 50, 10, 0); //left, top, right, bottom
        view_whwre_set_margin.setLayoutParams(params);

use this code to set margin at runtime

  LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) btnContainer.getLayoutParams();
        params.setMargins(5, 50, 10, 0); //left, top, right, bottom
        view_whwre_set_margin.setLayoutParams(params);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文