【问题标题】:Android - ListViews inside tableLayoutAndroid - tableLayout 内的 ListViews
【发布时间】:2012-02-10 14:28:45
【问题描述】:

我正在尝试构建和显示有组织的数据的应用程序,所以我认为基本上 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。我怎样才能解决这个问题?有没有其他方法可以轻松做到这一点?

【问题讨论】:

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


    【解决方案1】:

    所以我的理解是,没有拿起表格布局。我怎样才能解决这个问题?有没有其他方法可以轻松做到这一点?

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

    我会补充一点,你有一个只有一行的 TableLayout。 TableLayout 的设计目的是允许它们的内容根据它们的所有行来格式化它们的列。使用单个 Row,TableLayout 和 Row 一起充当 LinearLayout:

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

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

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

    【讨论】:

    • 哇,谢谢你的信息,真的很有启发性,我会这样做,看看会发生什么,但另一方面,如果我想稍后从数据库中的信息中添加更多行,那就是更好地以编程方式完成或在 xml 文件中设置布局?
    • 如果您要提取可变数量的行,您别无选择,只能以编程方式进行(因为您要么不够,要么会有多余的行)。如果你总是有一个固定的数量(即,你有“每页 5 个结果”),那么在 XML 中定义它们就是 find,你可以将额外的不用作 android:visibility="gone"。
    【解决方案2】:

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

    这个LinearLayout布局或者它的RelativeLayout父级是没用的

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

    【讨论】:

      【解决方案3】:

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

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

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

      【讨论】:

      • #2 不,只是在它的前面直接设置一个文本和一个列表,在同一行中。只是为了像记分卡或目录一样布局
      猜你喜欢
      • 2011-07-30
      • 2012-08-28
      • 2013-06-25
      • 2015-05-01
      • 2019-08-14
      • 1970-01-01
      • 2015-02-19
      • 2013-03-23
      • 1970-01-01
      相关资源
      最近更新 更多