TableLayout在模拟器上看起来很好但在设备上却没有

时间:2012-01-11 22:35:45

标签: android android-emulator tablelayout

我正在使用Android 2.2在模拟器上运行我的代码,它运行正常。但是,当我把它放在我的设备(Galaxy S 2.3.3)上时,应该在表格中显示元素列表的主屏幕保持空白。但是会显示Toast以及带有app_name

的标题

该表由一个按钮(以XML定义)和一个从DB加载的元素列表组成。

main.xml中

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TableLayout android:id="@+id/TableLayoutMain"
    android:layout_width="match_parent" android:layout_height="wrap_content">
    <TableRow>
        <Button android:text="@string/add_button" android:id="@+id/add_button"
            android:layout_height="wrap_content" android:layout_marginTop="10dp"
            android:layout_marginBottom="10dp" android:layout_width="match_parent"
            android:layout_weight="1"></Button>
    </TableRow>
</TableLayout>
</ScrollView>

这里是代码:

public void showList(Cursor c){

    setContentView(R.layout.main);      
    table = (TableLayout) findViewById(R.id.TableLayoutMain);

    do {
        TableRow tr = new TableRow(this);           
        final int id = c.getInt(c.getColumnIndex(DatabaseAdapter.KEY_ROWID));
        tr.setId(id);           
        tr.setBackgroundColor(ROW_COLOR);           

        tr.setOnLongClickListener(new View.OnLongClickListener() {

            @Override
            public boolean onLongClick(View v) {

                //do stuff

                return true;
            }
        });

        tr.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                //do other stuff
            }
        });                     

        TextView txt = getText(active, id, name);
        tr.addView(txt);
        table.addView(tr);          

    } while (c.moveToNext());
    c.close();
}

我不确定XML定义和以编程方式添加元素之间的混合是否有效。但我尝试了各种组合,可以说它们都同样糟糕。即在模拟器上很好,在设备上很糟糕。 我还用android 2.3.3创建了一个虚拟设备,其中代码运行良好......所以我猜它不能是Android版本。有什么想法吗?

1 个答案:

答案 0 :(得分:0)

完成,通过使用相对布局作为TableLayout的父级来使其工作。不完全是我想要的但它有效...