繁体   English   中英

在 Linearlayout 中动态添加 View 的问题

[英]Problem to add View dynamically in Linearlayout

我想在LinearLayout添加多个 View 。 这是我用于向LinearLayout添加多个视图的代码。

Java代码:

LinearLayout seriesMainListItemView = (LinearLayout) findViewById(R.id.SeriesMainListItemView);
                LayoutInflater mInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);

                for (int i=0; i<scheduleArr.length; i++) {

                      View inflatedView = mInflater.inflate(R.layout.scheduleitem, null);
                      TextView inflatertext1 = (TextView) inflatedView.findViewById(R.id.text1);
                      TextView inflatertext2 = (TextView) inflatedView.findViewById(R.id.text2);
                      inflatertext1.setText(scheduleArr[i][0]);
                      inflatertext2.setText(scheduleArr[i][1]);
                      Log.i("data",i + " " + scheduleArr[i][0] + "/" + scheduleArr[i][1]);
                      seriesMainListItemView.addView(inflatedView);
                }

这是我想多次添加的视图 xml。


这是我要添加的 LinearLayout。

<TableLayout
                android:layout_gravity="center_vertical" 
                android:layout_width="fill_parent" 
                android:layout_height="fill_parent"
                android:paddingLeft="1dip"
                android:paddingRight="1dip" >
                 <TableRow
                    android:layout_width="fill_parent"
                    android:layout_height="fill_parent">
                    <ImageView
                       android:id="@+id/imgSeriesMainScheduleImg"
                       android:layout_width="wrap_content"
                       android:layout_height="wrap_content"
                       android:layout_gravity="center"
                       android:background="@drawable/scheduleheader"/> 
                    <LinearLayout  
                        android:id="@+id/SeriesMainListItemView"                                    
                        android:layout_width="fill_parent"
                        android:layout_height="fill_parent">
                    </LinearLayout>                                                                                              
                </TableRow>
..............
</TableLayout>

但是,虽然数组的长度是 3,但只有单个视图添加到 LinearLayout 中。我的代码有什么问题?

我已将 LinearLayout 的方向添加为垂直。 它变得完美。 谢谢苏吉特的提示。

使用采用LayoutParams object 的addView()版本,提供适当的LinearLayout.LayoutParams ,看看是否有帮助。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM