以下代码改变TextView的margin和CENTER_HORIZONTAL属性。
代码语言:javascript复制 TextView textView=(TextView)findViewById(R.id.textView);
ViewGroup.MarginLayoutParams margin2 = new ViewGroup.MarginLayoutParams(textView.getLayoutParams());
margin2.setMargins(margin2.leftMargin, margin2.topMargin, margin2.rightMargin, margin2.bottomMargin);
RelativeLayout.LayoutParams layoutParams2 = new RelativeLayout.LayoutParams(margin2);
layoutParams2.addRule(RelativeLayout.CENTER_HORIZONTAL, RelativeLayout.TRUE);
textView.setLayoutParams(layoutParams2);