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

重复多次,必要时或使用循环

相关问题