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

あなたの質問を正しく理解した場合は、レイアウトにボタン、テキストビュー、ボタンを並べて表示する必要があります。これが必要な場合は、リストビューを取得してカスタムアダプターを使用するだけで、ボタン、テキスト、ボタンを並べて表示できます

于 2012-01-11T10:59:10.907 に答える