我按下按钮后,希望能以编程方式将文本字段从当前位置向左移动20像素。
这是我的 XML 文件:
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingTop="5dp"
android:paddingBottom="5dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/txtField"
android:layout_below="@+id/txtField2"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginLeft="20dp"
android:layout_toLeftOf="@+id/Seperator"
android:layout_toStartOf="@+id/Seperator"
android:layout_marginRight="10p" />
....
</RelativeLayout>
我尝试了这段代码:
parameter = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
parameter.setMargins(30, 32, 10, 0); // left, top, right, bottom
txtField.setLayoutParams(parameter);
目前这个功能达到了半优化的状态。 有没有一种方式可以在程序中使用XML文件中的所有值,并仅更改左边距的值?
LinearLayout
,则使用LinearLayout.LayoutParams
作为参数。 - M DEV