java代码控制layout_Margin参数

在Android开发中,遇到需要通过Java代码动态控制布局间距的问题。通常在XML中使用android:layout_margin设置,但Java中没有直接对应的setMargin方法。通过查阅资料,发现可以使用ViewGroup.MarginLayoutParams类的相关方法如setMargins来实现。然而,对于在Java中创建的LinearLayout,直接应用这些方法可能存在一些挑战,需要进一步探索合适的方法。
摘要由CSDN通过智能技术生成

声明

本文属于个人所有,转载注明出处:http://blog.csdn.net/hnulwt/article/details/43671687

问题描述

今天在做android的界面,不过需要用java代码来动态生成一个界面,写起来不算难,但是在控制每行与上行的间隔的时候遇到了问题。在layout xml文件中,通过书写android:layout_marginLeft android:layout_marginTop 等等几个方法来控制当前view与旁边view的间隔距离。但是如何在java中书写呢?

解决方案

在java代码中输写,我只发现了有setPadding(left, top, right, bottom),但是并没有setMargin或者setLayoutMargin方法来控制view间隔。
查询了谷歌官网,在官网查到了ViewGroup.MarginLayoutParams类,资料如下:


XML Attributes
Attribute Name Related Method Description
android:layout_marginBottom setMargins(int,int,int,int) Specifies extra space on the bottom side of this view.
android:layout_marginEnd setMarginEnd(int) Specifies extra space on the end side of this view.
android:layout_marginLeft setMargins(int,int,int,i

  • 2
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值