如何以编程方式将视图添加到RelativeLayout? [英] How to add a view programmatically to RelativeLayout?

查看:81
本文介绍了如何以编程方式将视图添加到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和构造函数的文档为

The docs for RelativeLayout.LayoutParams and the constructors are here

这篇关于如何以编程方式将视图添加到RelativeLayout?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆