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

所以我在哪裏出了錯了語法錯誤之後?請通過一些代碼來幫助我通過for循環添加它。

+0

我認爲你應該使用的ListView – 2012-01-11 10:56:39

+0

父也許你需要一個ListView和n一個LinearLayout。在這裏看到更多的信息http://developer.android.com/resources/tutorials/views/hello-listview.html – 2012-01-11 10:57:34

+0

@Samir:請看我更新的問題。 – 2012-01-11 11:03:14

回答

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); 
    } 
+0

感謝您的代碼爲我工作。 。 。 。 – 2012-01-11 12:05:19

+0

'parent.add(llItem);'不爲我編譯==''LinearLayout'沒有任何定義'add()',而是'addView()'。 – 2014-06-13 08:25:39

+0

對我來說,佈局是在同一條線上膨脹......我需要它逐行...你能幫助我嗎? – Benedict 2016-06-30 08:11:07

0

如果我正確理解你的問題,你需要在你的佈局中並排顯示按鈕,textview和按鈕。 如果你想要這個,你把ListView和使用自定義適配器,有了這個就可以顯示按鈕,通過側面的文字和按鈕側

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

重複多次,必要時或使用循環

相關問題