androdid设置View的Margin

来源:互联网 发布:我的世界枪械手机版js 编辑:程序博客网 时间:2024/06/05 04:40

在xml中有一个android:layout_margin用于设置View用于设置View的Margin,但是View没有setMargin方法。


通过查阅android api,我们发现android.view.ViewGroup.MarginLayoutParams有个方法setMargins(left, top, right, bottom)。
其直接的子类有: FrameLayout.LayoutParams, LinearLayout.LayoutParams and RelativeLayout.LayoutParams

使用方法:
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
lp.setMargins(10, 10, 10, 10);

view.setLayoutParams(lp);


原创粉丝点击