【问题标题】:TableLayout not working表格布局不起作用
【发布时间】:2013-03-25 16:11:47
【问题描述】:

我有一个TableLyout(在 XML 文件中),我正在尝试向其中添加表行。每行包含按钮,其编号由参数控制。

每次我运行这段代码时,程序都会退出。这应该是一个简单的任务。出了什么问题?

Java:

public class Play extends Activity{
int status=0 , timeDelay , row , col;
TextView score;
TableRow    rows [];
TableLayout  table;

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.layout_play);

    setLevel(); 
    table = (TableLayout)findViewById(R.id.Table_cards);

    for(int i=0 ; i<row ; i++){
        TableRow tblr = new TableRow(this);
        for(int j=0 ; j<col ; j++){
            final ImageButton b = new ImageButton(this);
            b.setOnClickListener(new OnClickListener() {

                public void onClick(View v) {
                    b.setBackgroundResource(R.drawable.card_back_blue);
                }
            });
            b.setBackgroundResource(R.drawable.c1+j);
            tblr.addView(b);
        }
        table.addView(tblr);
    }
}

XML 文件:

 <TableLayout
    android:id="@+id/Table_cards"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_below="@id/layout_up"
    android:background="@drawable/wss"
    android:gravity="center_vertical"
    android:orientation="vertical" >
</TableLayout>

【问题讨论】:

  • 你能把setLevel ()和你使用的任何其他支持功能和错误输出一起发布。
  • 你需要初始化rowcol变量ex: int row=5, col=5;

标签: android tablelayout tablerow


【解决方案1】:

这段代码运行良好,

    public class Play extends Activity{
    private int status=0 , timeDelay , row=10, col=10;
    private TextView score;
    private TableRow    rows [];
    private TableLayout  table;
    @Override
    public void onCreate(Bundle bundle)
    {
        super.onCreate(bundle);
        setContentView(R.layout.table_dynamic);

//      setLevel(); 
        table = (TableLayout)findViewById(R.id.tableLayout);

        for(int i=0 ; i<row ; i++){
            TableRow tblr = new TableRow(this);
            for(int j=0 ; j<col ; j++){
                final ImageButton b = new ImageButton(this);
                b.setOnClickListener(new OnClickListener() {

                    public void onClick(View v) {
//                      b.setBackgroundResource(R.drawable.card_back_blue);
                    }
                });
//              b.setBackgroundResource(R.drawable.c1+j);
                tblr.addView(b);
            }
            table.addView(tblr);
        }
    }
}

和布局table_dynamic,

<TableLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/tableLayout"
    android:background="@android:color/black"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
</TableLayout>

【讨论】:

  • 您是否会考虑添加一些叙述来解释此代码为何有效,以及是什么使它成为问题的答案?这对提出问题的人以及其他任何出现的人都非常有帮助。
  • 对不起,我会考虑的。
猜你喜欢
  • 2014-12-19
  • 2018-05-30
  • 1970-01-01
  • 1970-01-01
  • 2014-10-17
  • 1970-01-01
  • 1970-01-01
  • 2011-11-22
相关资源
最近更新 更多