【问题标题】:Clearing TableLayout inside LinearLayout removes TextView Header在 LinearLayout 中清除 TableLayout 会删除 TextView Header
【发布时间】:2012-07-15 23:44:05
【问题描述】:

我有一个嵌入了 TextView 和 TableLayout 的 LinearLayout。这是我的xml文件的格式。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" 
 >
 <TextView
        android:id="@+id/txtHeader"
        android:layout_width="match_parent"
        android:text="Header"  />
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:stretchColumns="1">
     <TableRow>
      <!-- Column 1 -->
      <TextView
         android:id="@+id/row1Item1"
         android:text="Column1" />
       <!-- Column 2 -->
      <TextView
         android:id="@+id/row1Item2"
         android:text="Column2" />
     </TableRow>
</TableLayout> 
</LinearLayout>  

我正在向表中动态添加行。每次,我得到一个更新,我需要清除表并写入新信息(行数是变量)。所以,每当我必须更新时,我都会尝试清除表格作为第一步。

    ll = (LinearLayout)findViewById(R.id.wlayout);
    tb = (TableLayout)findViewById(R.id.wtable);

private void on Update(){
tb.removeAllViewsInLayout();

Create table dynamically..
}

但它也删除了我的文本标题。我也尝试过 ll.removeView(tb),其中 ll 是 LinearLayout。谁能帮帮我吗。我是安卓新手。谢谢。

【问题讨论】:

  • 明白我的意思了吗??

标签: android android-linearlayout tablelayout


【解决方案1】:

您没有给 TableLayout 标签提供 id。请提供 id 然后尝试清除它。

 <TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="+@id/wtable"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:stretchColumns="1">
     <TableRow>
      <!-- Column 1 -->
      <TextView
         android:id="@+id/row1Item1"
         android:text="Column1" />
       <!-- Column 2 -->
      <TextView
         android:id="@+id/row1Item2"
         android:text="Column2" />
     </TableRow>
</TableLayout> 

然后试试这个..

tb = (TableLayout)findViewById(R.id.wtable);  
tb.removeAllViewsInLayout();

【讨论】:

  • 哦,我很抱歉..我确实给我的桌子一个 id。我只是想显示我的 xml 的格式。所以我在这里暂时删除了它。
  • 标题行也被删除了。有什么办法可以保留标题并删除动态创建的行
猜你喜欢
  • 1970-01-01
  • 2012-05-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-11-10
  • 2013-12-04
  • 1970-01-01
  • 2014-11-28
相关资源
最近更新 更多