如何programmattically视图添加到RelativeLayout的? [英] How to add a view programmattically to RelativeLayout?
问题描述
您能给我在指定位置添加子视图以编程方式 RelativeLayout的
的一个很简单的例子?
Can you give me a very simple example of adding child view programmatically to RelativeLayout
at a given position?
例如,要体现以下XML:
For example, to reflect the following XML:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="107dp"
android:layout_marginTop="103dp"
android:text="Large Text"
android:textAppearance="?android:attr/textAppearanceLarge" />
我不明白如何创造一个合适的 RelativeLayout.LayoutParams
实例。
I don't understand how to create an appropriate RelativeLayout.LayoutParams
instance.
推荐答案
下面有一个例子,让你开始,填写其他适用的:
Heres an example to get you started, fill in the rest as applicable:
TextView tv = new TextView(mContext);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.ALIGN_PARENT_LEFT, RelativeLayout.TRUE);
params.leftMargin = 107
...
mRelativeLayout.addView(tv, params);
有关RelativeLayout.LayoutParams的文档和构造函数是<一个href=\"http://developer.android.com/reference/android/widget/RelativeLayout.LayoutParams.html#RelativeLayout.LayoutParams%28int,%20int%29\"相对=nofollow>这里
The docs for RelativeLayout.LayoutParams and the constructors are here
这篇关于如何programmattically视图添加到RelativeLayout的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!