3

以前有人问过这个问题,但我还没有找到适合我的解决方案:

从代码向表中添加行时,这些行不会显示在应用程序中。我在 XML 中指定了一行,该行正在显示,但在其下方没有任何内容。

这是代码,我也添加了我的类的构造函数:

private LayoutInflater theInflater;
private LinkedList<View> rows;
private Context thisContext;

public DistanceTableView(LayoutInflater vi, Context context)
{
  theInflater = vi;
  rows = new LinkedList<View>();
  thisContext = context;    
}

public void addRow(LocationMessage locationMsg){
  View messageView = theInflater.inflate(R.layout.homepage, null);
  TableLayout table = (TableLayout)messageView.findViewById(R.id.distanceTable);

  TextView senderNameTextView = new TextView(thisContext);
  senderNameTextView.setText(locationMsg.getSenderName());

  TableRow tr = new TableRow(thisContext);
  tr.addView(distanceTextView);
  table.addView(tr);
  rows.addFirst(messageView);
}

homepage.xml 包含这个,我删除了一些元素和参数:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout>
    <TabHost>
    <TabWidget />
        <FrameLayout>
        [..]        
            <LinearLayout>          
            [..]
                <TableLayout
                android:id="@+id/distanceTable" 
                android:layout_height="wrap_content"
                android:layout_width="wrap_content"
                android:layout_gravity="center"
                android:background="#DDDDDD"
                android:stretchColumns="1" >
                    <TableRow>
                        <TextView
                        android:textColor="#000000"
                        android:text="@string/label_device"
                        android:layout_gravity="center"
                        android:padding="3dip"
                        android:textSize="18sp" />
                        <TextView
                        android:textColor="#000000"
                        android:text="@string/label_distance"
                        android:layout_gravity="center"
                        android:padding="3dip"
                        android:textSize="18sp" />
                        <TextView
                        android:textColor="#000000"
                        android:text="@string/label_time"
                        android:layout_gravity="center"
                        android:padding="3dip"
                        android:textSize="18sp" />
                    </TableRow>
                </TableLayout>
            </LinearLayout>
        </FrameLayout>
    </TabHost>
</LinearLayout>

不幸的是,hierarchyviewer.bat 不适用于我检查行是否存在但不可见。在调试器中它对我来说看起来不错。

4

1 回答 1

0

我认为您应该在添加新行后调用 TableLayout.requestLayout() 。

文档

当某些事情发生变化而导致该视图的布局无效时调用它。这将安排视图树的布局传递。

于 2011-01-31T15:42:35.653 回答