0

はここに私のPrentレイアウト(main.xml)既に作成されたLinerLayoutに別のレイアウトの時間を追加するには?ここで

<!-- ============================================================ --> 
      <!-- 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

で構文エラーを得たことをやった後、だからここで私が間違っていますか?ループのためにいくつかのコードを追加してください。

+0

は、私はあなたがリストビュー –

+0

を使うべきだと思うループを使用し、親におそらくあなたは、ListViewコントロールを必要とn LinearLayoutを使用します。詳細はこちらhttp://developer.android.com/resources/tutorials/views/hello-listview.html –

+0

@Samir:最新の質問をご覧ください。 –

答えて

2

によって、ボタン、テキストとボタン側を表示することができ あなたはこれをしたい場合は、単にリストビューを取ると、カスタムアダプタを使用し、

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); 

を繰り返し、必要な回数だけか

+0

あなたのコードは私のために働いてくれてありがとう。 。 。 。 –

+0

'parent.add(llItem);'私のためにコンパイルされない==> 'LinearLayout'は' add() 'の定義を持ちませんが、' addView() 'の定義はありません。 –

+0

私のために、レイアウトは同じ行で膨らんでいます...私は行ごとにそれを必要とします...これで私を助けることができますか? – Benedict

0

あなたの質問を正しく理解していれば、レイアウトにボタン、テキストビュー、ボタンを並べて表示する必要があります。その後、追加

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); 
    } 
1

は、ビューを膨らま:これであなたはただ、下記のようなループを作成する側

関連する問題