android以编程方式设置保证金

堆栈108:

我按下按钮后,我想以编程方式从实际位置向左移动文本字段+ 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文件的所有值,并且仅以编程方式更改边距剩余值?

Dieter_h:

试试这个代码:

parameter =  (RelativeLayout.LayoutParams) txtField.getLayoutParams();
parameter.setMargins(leftMargin, parameter.topMargin, parameter.rightMargin, parameter.bottomMargin); // left, top, right, bottom
txtField.setLayoutParams(parameter);

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章