用代码设置Layout_margins

来源:互联网 发布:淘宝店铺装修首页大图 编辑:程序博客网 时间:2024/05/16 03:59

我在写用代码生成一个UI布局,发现不能直接setMargins,然后网上查了一下,发现解决方案如下:

 LinearLayout linLayout = new LinearLayout();

TextView text = new TextView(context);
text.setGravity(Gravity.);   //设置Gravity参数(注意不是LayoutGravity,Gravity和LayoutGravity他们之间的区别,百度你懂的。)
text.setText(content);
text.setTextSize(16);
text.setTextColor(Color.BLACK);

LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT);
lp.setMargins(25, 25, 25, 25);

textView.setGravity(Gravity.CENTER);

textView.setLayoutParams(lp);

textView.addView(text);