使用布局作为模板来创建多个布局实例的Andr​​oid [英] Android using layouts as a template for creating multiple layout instances

查看:134
本文介绍了使用布局作为模板来创建多个布局实例的Andr​​oid的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好了,所以我知道如何使用包括标记,但我碰到的一个问题。

Ok so I understand how to use the include tag but I've run into a problem.

基本上我想有XML其中有几个textviews,并在它的图像视图定义的布局。那么我想整个数组进行迭代,并根据最新的阵列(其中填充在运行时)填充中的XML布局领域。这样做的XML布局多个副本和填充具有独特的数据的字段。现在我已经得到了不知道如何可以重新使用的LinearLayout以这种方式作为其中的textviews和imageviews具有恒定的ID,我需要做此布局的多个副本。

Basically I want to have a layout defined in xml which has a couple of textviews and an image view in it. I then want to iterate across an array and populate fields within the xml layout depending on whats in an array(which is populated on runtime). Thus making multiple copies of the xml layout and populating the fields with unique data. Now i've got no idea how you can re-use this linearlayout in this way as the textviews and imageviews within it have a constant id and I need to make multiple copies of this layout.

有没有什么办法可以填充的资源,然后作出它的一个副本,将工作......于是

Is there any way to inflate a resource and then make a copy of it, that would work... So

的LinearLayout 1 =新的LinearLayout(inflater.inflate(R.layout.home,集装箱,FALSE));

LinearLayout one = new LinearLayout(inflater.inflate(R.layout.home, container, false));

^即使世界一样,没有构造很遗憾。

^ Theres no constructor like that unfortunately.

其他唯一的办法就是做这一切,但编程的preferred我有意见的性质和XML,而不是在code中的LinearLayout中。这就像我想的LinearLayout是一个模板,你可以拷贝我猜......真的不知道如果多数民众赞成可能的。

The only other way is to do it all programatically but I would of preferred to have the properties of the views and the linearlayout in xml rather than in the code. It's like I want the linearlayout to be a template which you can make copies of I guess... Really not sure if thats possible.

谢谢!

推荐答案

您可以轻松地做到这一点,你就必须要打破它。首先,你加载你要插入动态视图到布局。然后,你吹你的子视图,并填充它多次,你所需要的。然后你的视图添加到您的父母的布局,最后设置活动父视图的内容视图。

You can easily do this, you just have to break it down. First you load the layout that you want to insert your dynamic views into. Then you inflate your subview and populate it as many times as you need. Then you add the view to your parent layout, and finally set the content view of the activity to the parent view.

下面是一个例子:

    LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    LinearLayout parent = (LinearLayout) inflater.inflate(R.layout.main,
            null);

    for (int i = 0; i < 3; i++) {
        View custom = inflater.inflate(R.layout.custom, null);
        TextView tv = (TextView) custom.findViewById(R.id.text);
        tv.setText("Custom View " + i);
        parent.addView(custom);
    }

    setContentView(parent);

下面是我插入的main.xml中的文件:

here is the main.xml file that I am inserting into:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

</LinearLayout>

和这里是我膨胀,填充和动态插入custom.xml观点:

and here is the custom.xml view that I inflate, populate and dynamically insert:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal" >

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

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/ic_launcher" />

        <TextView
            android:id="@+id/text"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content" />
    </LinearLayout>

</LinearLayout>

希望这个例子可以帮助!

Hope this example helps!

这篇关于使用布局作为模板来创建多个布局实例的Andr​​oid的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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