【问题标题】:Problem to add View dynamically in Linearlayout在 Linearlayout 中动态添加 View 的问题
【发布时间】:2011-07-20 11:09:54
【问题描述】:

我想在LinearLayout 中添加多个视图。这是我用于向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);
                }

这是我要多次添加的 View 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>

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

【问题讨论】:

  • 你设置了LinearLayout的方向了吗?

标签: android view android-linearlayout


【解决方案1】:

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

【讨论】:

  • 这解决了我的问题!我试图制作一个自定义的 android 组件,但忘记在扩展 LinearLayout 的新类上设置方向。
【解决方案2】:

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

【讨论】:

  • 我试过“seriesMainListItemView.addView(inflatedView,i,new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));”同样,结果没有变化
  • @dalandroid:使用层次视图查看是否正在添加小部件。如果不是,请确定为什么您的代码没有被调用或其他原因。如果是,层次结构视图应指出它们不可见的原因。
猜你喜欢
  • 2020-07-26
  • 2018-10-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多