0
点赞
收藏
分享

微信扫一扫

Android,View设置margin

ITWYY 2023-03-16 阅读 63


以把设置margin的方式封装成方法,只要是GroupView里面的LayoutParams 即可。

public static void setMargins (View v, int l, int t, int r, int b) {
if (v.getLayoutParams() instanceof ViewGroup.MarginLayoutParams) {
ViewGroup.MarginLayoutParams p = (ViewGroup.MarginLayoutParams) v.getLayoutParams();
p.setMargins(l, t, r, b);
v.requestLayout();
}
}

使用

TextView tv1 = new TextView(this);
tv1.setText(getResources().getString(R.string.user_agreement));
tv1.setTextSize(14);
tv1.setTextColor(Color.BLUE);

TextView tv2 = new TextView(this);
tv2.setText(getResources().getString(R.string.privacy_policy));
tv2.setTextSize(14);
tv2.setTextColor(Color.BLUE);
;
LinearLayout linearLayout = new LinearLayout(this);
linearLayout.setOrientation(LinearLayout.VERTICAL);
linearLayout.setGravity(Gravity.CENTER);
linearLayout.addView(tv1);
linearLayout.addView(tv2);
setMargins(tv1,50, 20, 0, 0);
setMargins(tv2,50, 20, 0, 0);

 

举报

相关推荐

0 条评论