安卓代码动态改变控件位置和属性

2020-02-12 16:37:50 浏览数 (1)

以下代码改变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);

0 人点赞