2016-12-15 63 views
0

我有一个LinearLayout的xml文件,它包含一个TableLayout,后面包含TableRows,所以我希望在应用程序运行时以编程方式添加一个新的TableRow。TableLayout addView不起作用

所以我写了下面的代码使用addView方法,但我的代码不起作用。

public class MainActivity extends Activity 
{ 
    @Override 
protected void onCreate(Bundle savedInstanceState) 
{ 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    LinearLayout myRoot = (LinearLayout)findViewById(R.id.tbl_test); 
    TableRow a = new TableRow(this); 
    a.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)); 
    myRoot.addView(a); 
} 
    } 

的xml文件

 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
     xmlns:tools="http://schemas.android.com/tools" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:padding="10dp" 
     android:orientation="vertical"> 


     <TableLayout 
     android:id="@+id/tbl_test" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:background="#000000" 
     android:orientation="vertical" 
     android:layout_marginTop="1dp" 
     android:layout_marginBottom="1dp" 
     android:stretchColumns="1" 
           > 

    <TableRow 
    android:id="@+id/tbr_test" 
    android:background="#ffffff" android:layout_margin="1dp"> 

    <TextView android:layout_column="0" /> 
    <TextView android:layout_column="1" /> 
    <TextView android:layout_column="2" /> 
    <TextView android:layout_column="3" /> 

    </TableRow> 

    <TableRow 
    android:background="#ffffff" 
    android:layout_marginLeft="1dp" 
    android:layout_marginRight="1dp" 
    android:layout_marginBottom="1dp"> 

    <TextView android:layout_column="0" /> 
    <TextView android:layout_column="1" /> 
    <Button/> 
    <Button/> 

    </TableRow> 

    <TableRow 
    android:background="#ffffff" 
    android:layout_marginLeft="1dp" 
    android:layout_marginRight="1dp" 
    android:layout_marginBottom="1dp" 
    android:paddingRight="2dp"> 

    <TextView android:layout_column="0" /> 
    <TextView android:layout_column="1" /> 
    <Button/> 
    <Button /> 

    </TableRow> 

    <TableRow 
    android:background="#ffffff" 
    android:layout_marginLeft="1dp" 
    android:layout_marginRight="1dp" 
    android:layout_marginBottom="1dp" 
    android:paddingRight="3dp"> 

    <TextView android:layout_column="0" /> 
    <TextView android:layout_column="1" /> 
    <Button /> 
    <Button /> 

    </TableRow> 

    <TableRow 
    android:id="@+id/tbr" 
    android:background="#ffffff" 
    android:layout_marginLeft="1dp" 
    android:layout_marginRight="1dp" 
    android:layout_marginBottom="1dp" 
    android:paddingRight="4dp"> 

    <TextView android:layout_column="0" /> 
    <TextView android:layout_column="1" /> 
    <Button /> 
    <Button/> 

    </TableRow> 

    </TableLayout> 

    </LinearLayout> 

回答

0

TableLayout.LayoutParams导致问题。所以你必须使用LayoutParams而不是LinearLayout.LayoutParams

所以你的这个代码

TableRow a = new TableRow(this); 
a.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)); 
myRoot.addView(a); 

改成这样

TableRow a = new TableRow(this); 
a.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)); 
myRoot.addView(a); 

,并给予一定的背景颜色或添加一些成分在增加TableRow看到的。

+0

不,它不适用于我 – JavaFan

+0

@JavaFan添加此后您有什么问题。 – Ironman

+0

没问题,但没有添加新行可能是因为最小api设置为8?但我添加这一行@TargetApi(Build.VERSION_CODES.HONEYCOMB) – JavaFan