Android:如何使用代码更改textview的布局?
在setContentView(R.layout.activity_main)之后,我想使用代码只更改textview的layout_marginTop(android:layout_centerHorizontal=“true”) 居中更改为左对齐。Android:如何使用代码更改textview的布局?,android,Android,在setContentView(R.layout.activity_main)之后,我想使用代码只更改textview的layout_marginTop(android:layout_centerHorizontal=“true”) 居中更改为左对齐。 setMargins(int left、int top、int right、int bottom)包含4个参数,但我只想更改第二个参数 尝试更换 RelativeLayout.LayoutParams lp = new RelativeLayou
setMargins(int left、int top、int right、int bottom)包含4个参数,但我只想更改第二个参数 尝试更换
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,RelativeLayout.LayoutParams.WRAP_CONTENT);
为此:
RelativeLayout.LayoutParams lp = (RelativeLayout.LayoutParams)tvcity.getLayoutParams();
尝试使用现有页边距:
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,RelativeLayout.LayoutParams.WRAP_CONTENT);
lp.setMargins(lp.leftMargin, 20, lp.rightMargin, lp.bottomMargin);
tvcity.setLayoutParams(lp);
那么lp.topMargin=100;
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,RelativeLayout.LayoutParams.WRAP_CONTENT);
lp.setMargins(lp.leftMargin, 20, lp.rightMargin, lp.bottomMargin);
tvcity.setLayoutParams(lp);