0

这是我的 Prent 布局(main.xml)

<!-- ============================================================ -->
            <!--  All Employees are added here (added and More then one) -->
            <!-- ============================================================ -->
            <LinearLayout 
                xmlns:android="http://schemas.android.com/apk/res/android"
                android:id="@+id/myLayout"
                android:orientation="vertical"
                android:layout_gravity="center_vertical"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:background="#00000000">

                <!-- ++++++++ here extra layout is added programmatically -->


            </LinearLayout>

这是我作为孩子想要的另一个布局文件,比如 child.xml

    <?xml version="1.0" encoding="utf-8"?>
<LinearLayout         
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/linearLayout2"         
    android:layout_width="fill_parent"         
    android:layout_height="wrap_content"         
    android:layout_alignParentLeft="true"         
    android:layout_alignParentTop="true"         
    android:gravity="center_vertical"         
    android:orientation="horizontal" >          

    <Button             
        android:id="@+id/btn_left"             
        android:layout_width="100dp"             
        android:layout_height="wrap_content" 
        android:singleLine="false"   
        android:maxLines="2"         
        android:text="hello"/>
    <TextView             
        android:id="@+id/list_title"             
        android:layout_width="fill_parent"             
        android:layout_height="wrap_content"             
        android:layout_weight="0.5"             
        android:gravity="center_horizontal"             
        android:text="Photos"             
        android:textAppearance="?android:attr/textAppearanceLarge" />          
    <Button             
        android:id="@+id/btn_right"             
        android:layout_width="100dp"             
        android:layout_height="wrap_content"             
        android:text="Save" />     
</LinearLayout>

现在在我的主要活动中,我正在调用 main.xml,并且基于 for 循环条件,我想添加 child.xml 的布局,并且每次我想设置 child.xml 的 TextView 的不同值时

那么它怎么可能呢?

我已经这样做了:

private void doCalculationForMultipleEmployee() {
    singleEmployee.setVisibility(View.GONE);
    for (int i = 0; i<=tempEmployerList.size()-1; i++) {  
        View repeatedLayout = LayoutInflater.from(getApplicationContext()).inflate(R.layout.test);     
        ((TextView)repeatedLayout.findViewById(R.id.list_title)).setText("Employee"+i);     
        // customize repeatedLayout with other data     
        myLinearLayout.addChild(repeatedLayout);
    }
}

但在这样做之后,我在 .inflate 和 .addChild 处出现语法错误

那么我哪里出错了?请通过一些代码帮助我通过for循环添加它。

4

3 回答 3

2

只需创建一个如下所示的循环:

    LinearLayout parent=findViewById(R.id.myLayout);
    for(int i=0;i<list.size();i++)
    {
       LinearLayout llItem=(LinearLayout)LayoutInflater.createFromSource(R.layout.child, null);
       TextView txt= (TextView)llItem.findViewById(R.id.title);
       txt.setText(list.get(i));
       parent.add(llItem);
    }
于 2012-01-11T11:04:25.380 回答
1

膨胀视图然后添加到父视图

LinearLayout parent = (LinearLayout)findViewById(R.id.myLayout);

LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
Layout child = inflater.inflate(R.layout.myLayout2, null);
TextView title = (TextView)child.findViewById(R.id.list_title);
parent.addView(child);

根据需要重复多次或使用循环

于 2012-01-11T11:05:11.463 回答
0

如果我正确理解了您的问题,您需要在布局中并排显示按钮、文本视图和按钮。如果你想要这个,你只需使用 listview 并使用自定义适配器,这样你就可以并排显示按钮、文本和按钮

于 2012-01-11T10:59:10.907 回答