Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/extjs/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 在同一父布局中动态克隆LinearLayout及其内容_Android - Fatal编程技术网

Android 在同一父布局中动态克隆LinearLayout及其内容

Android 在同一父布局中动态克隆LinearLayout及其内容,android,Android,如何以编程方式获取元素并在同一视图中多次克隆它? 我把这个LinearLayout放在RelativeLayout中,我想克隆我以前在xml文件中编写的LinearLayout,大约在RelativeLayout中克隆10次 这是结构: 代码如下: <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:lay

如何以编程方式获取元素并在同一视图中多次克隆它?
我把这个
LinearLayout
放在
RelativeLayout
中,我想克隆我以前在xml文件中编写的
LinearLayout
,大约在
RelativeLayout
中克隆10次

这是结构:

代码如下:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/bg_android"
    android:weightSum="1">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_margin="10dp"
        android:id="@+id/relativeLayout">

        <ImageView
            android:layout_width="180dp"
            android:layout_height="62dp"
            android:id="@+id/imageView2"
            android:src="@drawable/cartuxa_logo"
            android:layout_alignParentTop="true"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true" />

        <ImageButton
            android:layout_width="30dp"
            android:layout_height="30dp"
            android:id="@+id/menu_btn"
            android:layout_centerVertical="true"
            android:layout_alignParentRight="true"
            android:layout_alignParentEnd="true"
            android:background="@drawable/menu_icon" />

    </RelativeLayout>

    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="fill_parent"
        android:layout_height="230dp"
        android:gravity="left"
        android:id="@+id/linearLayout_ID"
        android:layout_below="@+id/relativeLayout"
        android:layout_alignLeft="@+id/relativeLayout"
        android:layout_alignStart="@+id/relativeLayout">

        <ImageView
            android:layout_width="120dp"
            android:layout_height="120dp"
            android:id="@+id/ll_preview_ID"
            android:background="#ff393939"
            android:layout_marginRight="4dp" />

        <ImageView
            android:layout_width="100dp"
            android:layout_height="fill_parent"
            android:id="@+id/ll_image_ID"
            android:background="#ff767676"
            android:contentDescription="@string/dfg" />


    </LinearLayout>


</RelativeLayout>


在Eclipse中,您可以右键单击布局并使用选项提取包含。(您也可以手动操作。)它会将您的布局提取到一个单独的XML中,并使您能够使用

再次包含新创建的布局。我建议您为要在relativelayout中不断添加的布局创建一个单独的布局XML文件。 完成此操作后,可以以这种方式添加布局

LayoutInflater inflater = (LayoutInflater)getBaseContext() .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
 RelativeLayout main =(RelativeLayout)findViewById(R.id.layout1); 

for(int i=0;i<9;i++){ 
View view = inflater.inflate(R.layout.sub_layout, null);
 main.addView(view);
 }
LayoutInflater充气器=(LayoutInflater)getBaseContext().getSystemService(Context.LAYOUT\u充气器\u SERVICE);
RelativeLayout main=(RelativeLayout)findViewById(R.id.layout1);
对于(int i=0;i