简体   繁体   English

如何在布局中创建“ i”个对象(通过循环)?

[英]How to create 'i' number of objects in a layout (by looping)?

I need to create i number of photo_view and give it a special amount every time (loop). 我需要创建i一些photo_view每一次(循环)给它一个特殊的量。 number of photo_view in every time will be different. 每次的photo_view数量将有所不同。 What exactly should I do in the code ? 我到底应该在代码中做什么?

my xml file : 我的xml文件:

<?xml version="1.0" encoding="utf-8"?>
    <android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:custom="http://schemas.android.com/tools"
        android:id="@+id/coordinatorLayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:background="@color/black">

        <HorizontalScrollView
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="horizontal">

                <com.github.chrisbanes.photoview.PhotoView
                    android:id="@+id/photo_view"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"/>
            </LinearLayout>
        </HorizontalScrollView>

        <ImageView
            android:id="@+id/exit_btn"
            android:layout_width="25dp"
            android:layout_height="25dp"
            android:layout_margin="20dp"
            android:clickable="true"
            android:contentDescription="@string/app_name"
            android:src="@drawable/ic_back_ltr" />

    </android.support.design.widget.CoordinatorLayout>

Dynamically adding Views to a ViewGroup is what you are looking for. 您正在寻找将视图动态添加到ViewGroup方法。 which is done by code not by XML. 这是通过代码而不是XML来完成的。

In general, there are two ways to add a view dynamically, either by inflating some layout file in a view then add this view to your parent viewgroup ( LinearLayout for ex) by calling the add method on this parent view. 在一般情况下,有两种方式来添加view动态,无论是在充气一些布局文件view ,然后这个添加view到父viewgroupLinearLayout通过调用为前) add在这个父视图的方法。

Or by defining a new object of that view and add all params needed to it then add it to the parent viewgroup . 或者,通过定义该视图的new对象并向其添加所需的所有参数,然后将其添加到父viewgroup

See an example about way 1 here 在此处查看有关方法1的示例

See an example about way 2 here 在此处查看方法2的示例


in your case you could create a new layout.xml file contains only your custom view com.github.chrisbanes.photoview.PhotoView with all the needed params. 在您的情况下,您可以创建一个仅包含自定义视图com.github.chrisbanes.photoview.PhotoView以及所有所需参数的新layout.xml文件。 then add an id for the parent linearlayout 然后为父linearlayout添加一个ID

        <LinearLayout
            android:"@+id/parentViewGroup"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="horizontal">


        </LinearLayout>

And in your code, loop by the count and add the custom inflated view to parent. 然后在代码中按计数循环,然后将自定义展开视图添加到父级。 like this: 像这样:

View inflatedView = View.inflate(context, yourViewXML, null);
LinearLayout parentViewGroup = findViewById(R.id.parentViewGroup);

for(int i = 0 ; i<count ; i++){
  parentViewGroup.add(inflatedView);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM