gpt4 book ai didi

java - 动态编写 XML

转载 作者:行者123 更新时间:2023-12-01 13:40:16 33 4
gpt4 key购买 nike

我想知道如何用Java动态编写下面的数据。我对你如何编写“layout_alignParent”可以设置每个 TextView 的重力感到困惑......

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView2"
android:layout_alignParentBottom="true"
android:gravity="center_horizontal" />

<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:gravity="center_vertical" />

<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/textView3"
android:layout_alignBottom="@+id/textView3"
android:layout_alignParentRight="true"
android:gravity="center_vertical" />

<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:gravity="center_horizontal" />

我知道下面的代码不起作用。但我怎样才能重写它才能工作呢?

RelativeLayout m = (RelativeLayout)findViewById(R.id.tileContainerME);
EditText et1 = new EditText(this);
EditText et2 = new EditText(this);
EditText et3 = new EditText(this);
EditText et4 = new EditText(this);

RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
android.widget.RelativeLayout.LayoutParams.WRAP_CONTENT,
android.widget.RelativeLayout.LayoutParams.WRAP_CONTENT);

params.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, RelativeLayout.TRUE);
et1.setLayoutParams(params);
params.removeRule(RelativeLayout.ALIGN_PARENT_BOTTOM);

params.addRule(RelativeLayout.ALIGN_PARENT_LEFT, RelativeLayout.TRUE);
params.addRule(RelativeLayout.CENTER_VERTICAL, RelativeLayout.TRUE);
et2.setLayoutParams(params);
params.removeRule(RelativeLayout.ALIGN_PARENT_LEFT);
params.removeRule(RelativeLayout.CENTER_VERTICAL);

params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT, RelativeLayout.TRUE);
et3.setLayoutParams(params);
params.removeRule(RelativeLayout.ALIGN_PARENT_RIGHT);

params.addRule(RelativeLayout.ALIGN_PARENT_TOP, RelativeLayout.TRUE);
params.addRule(RelativeLayout.CENTER_HORIZONTAL, RelativeLayout.TRUE);
et4.setLayoutParams(params);
params.removeRule(RelativeLayout.ALIGN_PARENT_TOP);
params.removeRule(RelativeLayout.CENTER_HORIZONTAL);

et1.setGravity(Gravity.CENTER_HORIZONTAL);
et1.setHint("Test");
et2.setGravity(Gravity.CENTER_VERTICAL);
et2.setHint("Test");
et3.setGravity(Gravity.CENTER_VERTICAL);
et3.setHint("Test");
et4.setGravity(Gravity.CENTER_HORIZONTAL);
et4.setHint("Test");

m.addView(et1);
m.addView(et2);
m.addView(et3);
m.addView(et4);

最佳答案

我认为这段代码会对您有所帮助:

RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.ALIGN_PARENT_LEFT, RelativeLayout.TRUE);
textView.setLayoutParams(params);

编辑:

params.gravity = Gravity.CENTER_HORIZONTAL;
tv2.setLayoutParams(params);

关于java - 动态编写 XML,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20873335/

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