Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/195.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
Java 动态编写XML_Java_Android_Eclipse - Fatal编程技术网

Java 动态编写XML

Java 动态编写XML,java,android,eclipse,Java,Android,Eclipse,我想知道如何用Java动态编写下面的数据。我一直在思考如何编写“layout\u alignParent”来设置每个文本视图的重力 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" > <TextView andr

我想知道如何用Java动态编写下面的数据。我一直在思考如何编写“layout\u alignParent”来设置每个文本视图的重力

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

“下面的代码”不是“代码”,而是数据。XML数据。一旦理解了这一点,就很容易了,因为每个XML元素都对应于对象或其父对象的一个属性,您可以从文档中确定该属性。一个简单的谷歌搜索将提供许多如何在代码中设置这些属性的例子。现在我明白了这是数据,我通过你给我的链接查看了一下,它只向我展示了我需要的方法,但我想知道如何通过一个简单的例子来实现它们。你能给我一个吗?克斯特亚的回答是正确的。谢谢!地心引力会写成这样吗?textView.setGravity(重力。水平中心);然后我如何删除为其他文本视图声明的规则?
params.gravity = Gravity.CENTER_HORIZONTAL;
tv2.setLayoutParams(params);