【问题标题】:Android using layouts as a template for creating multiple layout instancesAndroid 使用布局作为模板创建多个布局实例
【发布时间】:2011-12-16 12:31:36
【问题描述】:

好的,我明白了如何使用包含标签,但我遇到了问题。

基本上,我想在 xml 中定义一个布局,其中包含几个 TextViews 和一个 ImageView。然后,我想遍历一个数组并根据数组中的内容(在运行时填充)填充 xml 布局中的字段。从而制作 xml 布局的多个副本并使用唯一数据填充字段。现在我不知道如何以这种方式重用这个LinearLayout,因为其中的TextViews 和ImageViews 有一个恒定的ID,我需要制作这个布局的多个副本。

有没有什么方法可以膨胀资源然后复制它,这会起作用......所以

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

^ 不幸的是,没有这样的构造函数。

唯一的另一种方法是以编程方式完成所有操作,但我更愿意在 xml 中而不是在代码中拥有视图和 LinearLayout 的属性。就像我希望 LinearLayout 成为一个模板,我猜你可以复制它......真的不确定这是否可能。

【问题讨论】:

    标签: android xml layout reusability


    【解决方案1】:

    您可以轻松做到这一点,您只需将其分解即可。首先,您加载要插入动态视图的布局。然后你膨胀你的子视图并根据需要多次填充它。然后将视图添加到父布局中,最后将活动的内容视图设置为父视图。

    这是一个例子:

    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 文件:

    <?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 视图:

    <?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>
    

    【讨论】:

    • 非常感谢!就这样……我想我需要了解不同的 inflate(....) 调用。
    • 谢谢!充气机的好例子!
    • 有没有办法在循环期间简单地复制布局,而不是每次迭代都使用 inflator.inflate() ?在我看来,与简单的复制相比,膨胀是非常昂贵的。
    • 是的,就是这样。谢谢
    • 有什么方法可以在片段中使用这个? getActivity().setContentView(parent 搞砸了整个事情,但我看到了结果。在实现这个 android.content.res.Resources$NotFoundException: String resource ID #0x7f0e00a2 之后,我似乎得到了很多这个错误
    【解决方案2】:

    对于仍在寻找类似解决方案的任何人,显然您也可以直接在 xml 中使用 include 并且仍然可以在代码中引用它们:

    LinearLayout row1 = (LinearLayout) findViewById(R.id.row1)
    TextView text1 = row1.findViewById(R.id.text);
    
    LinearLayout row2 = (LinearLayout) findViewById(R.id.row2)
    TextView text2 = row2.findViewById(R.id.text);
    

    来源:Romain Guy

    【讨论】:

      猜你喜欢
      • 2016-08-17
      • 1970-01-01
      • 2013-05-03
      • 1970-01-01
      • 2018-09-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多