TableLayout 在模拟器上看起来不错,但在设备上则不然
我正在 Android 2.2 的模拟器上运行我的代码,并且运行良好。但是当我把它放在我的设备(Galaxy S 2.3.3)上时,应该显示表格中元素列表的主屏幕 - 保持空白。然而,会显示 Toast 以及带有 app_name 的标题
该表由一个按钮(在 XML 中定义)和从数据库加载的元素列表组成。
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 版本。有什么想法吗?
I'm running my code on the emulator with Android 2.2 and it works fine. But when I put it on my device (Galaxy S 2.3.3) the main screen that should show a list of elements in a table - stays blank. Toasts however are shown and also the header with the app_name
The table is made of a button (defined in XML) and a list of elements loaded from a 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>
And here the code:
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();
}
I wasn't sure if the mixing between XML definitions and programatically adding elements would work. But I have tried various combinations and can say that they both work equally bad. i.e. fine on the emulator and bad on the device.
I have also created a virtual device with android 2.3.3 where the code runs fine as well... so I guess it can't be the Android version. Any thoughts?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
完成后,通过使用相对布局作为 TableLayout 的父级使其正常工作。不完全是我想要的,但它有效......
Done, got it to work by using a Relative layout as parent for the TableLayout. Not exactly what I wanted but it works...