【问题标题】:TableLayout addView doesn't workTableLayout addView 不起作用
【发布时间】:2016-12-15 18:46:59
【问题描述】:

我有一个 LinearLayout 的 xml 文件,其中包括一个 TableLayout,后者包括 TableRows,所以我希望在应用程序运行时以编程方式添加一个新的 TableRow。

所以我使用 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>

【问题讨论】:

    标签: android


    【解决方案1】:

    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 中查看。

    【讨论】:

    • @JavaFan 添加这个后你遇到了什么问题。
    • 没问题但没有添加新行可能是因为最小api设置为8?但我添加了这一行 @TargetApi(Build.VERSION_CODES.HONEYCOMB)
    • 我应该做这样的事情 true a.setBackgroundColor(Color.GREEN);?
    • @JavaFan 如果您想查看代码是否有效,请给它BackgroundColor
    • @JavaFan 这样添加Resources resource = context.getResources(); a.setBackgroundColor(resource.getColor(R.color.red).
    猜你喜欢
    • 2011-05-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-03-16
    • 1970-01-01
    相关资源
    最近更新 更多