Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/194.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android:如何使用代码更改textview的布局?_Android - Fatal编程技术网

Android:如何使用代码更改textview的布局?

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

在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 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);