gpt4 book ai didi

android以编程方式设置边距

转载 作者:IT老高 更新时间:2023-10-28 23:30:04 25 4
gpt4 key购买 nike

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

最佳答案

试试这个代码:

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

关于android以编程方式设置边距,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36114872/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com