Android - tableLayout中的ListViews

时间:2012-02-10 14:28:45

标签: android android-layout android-listview android-ui android-tablelayout

我正在尝试构建和显示有组织数据的应用程序,所以我认为基本上TableLayout会是一个好主意,只是为了让它们成行,所以我想知道什么是正确的方法去做吧?我在我的xml

中有这个
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <TableLayout
        android:id="@+id/tableLayout1"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">
        <TableRow>
            <TextView
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:text="@string/hello" >
             </TextView>

            <ListView
                android:id="@+id/listView1"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginLeft="233dp"
                android:layout_marginTop="44dp" >
            </ListView>
        </TableRow>
    </TableLayout>

</LinearLayout>

然后只是TableLayout代码和TableRow代码会显示一条警告:

"This TableLayout layout or its LinearLayout parent is possibly useless"

所以我理解的是没有拿起TableLayout。我怎样才能解决这个问题?还有另一种方法可以轻松地做到这一点吗?

3 个答案:

答案 0 :(得分:1)

  

所以我理解的是,这并没有取得桌面布局。我怎样才能解决这个问题?还有另一种方法可以轻松地做到这一点吗?

不完全是,渲染器 考虑了TableLayout,但因为lint工具检测到只有一个(A LinearLayout时有两个视图TableLayout)它警告您应该删除其中一个布局,因为它会在不影响页面功能的情况下提升性能。

我将补充一下,你只有一行TableLayout。 TableLayout旨在允许其内容根据其所有行格式化列。使用单行TableLayout&amp;行在一起充当LinearLayout:

<LinearLayout>
    <LinearLayout>
        <ListView>
        <TextView>

由于您的TableLayout在这种情况下没有添加任何添加布局,因此它已经过时,您可以通过删除它并将LinearLayout的orientation更改为horizontal来获得资源。

如果您计划稍后添加更多TableRow,那么您应该出于同样的原因删除LinearLayout - 它将成为不添加额外信息的视图:表格布局将完全负责布局,因此您可以将其作为布局文件的父级(如果您这样做,请记得向其添加xmlns:android="http://schemas.android.com/apk/res/android"属性。)

答案 1 :(得分:1)

总的来说,我看到了更强烈的警告:

  

此LinearLayout布局或其RelativeLayout父级是无用的

如果不是这样的话。例如,如果我在相对布局中嵌套了线性布局,就会发生这种情况。相对布局将线性布局精确定位在我想要的位置,线性布局占用空间。两者对我来说都是无用的。

答案 2 :(得分:0)

1)它说可能所以避免得出结论,相信自己年轻的padawan!但是,是的,父母对我来说也没用:)

2)使用ListView的表格布局 ,不会更改列表中的行布局,以防这是您想要实现的目标。

您可能已经看过这个,但Developers page offers a really good tutorial可以帮助您为ListView创建一个良好的基础(此示例使用ListActivity)。然后,您可以使用TableLayout等修改行的布局。