【问题标题】:Why does the compiler say I'm not giving my TableLayout an id when I am?为什么编译器说我没有给我的 TableLayout 一个 id?
【发布时间】:2012-03-14 05:05:31
【问题描述】:

我重新安排了动态创建小部件/视图的代码,虽然 xml 布局看起来“格式正确”(它可以编译),但当我到达相应的“setContentView()”时它崩溃了。

IOW:

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.ondemandandautomatic_dynamicauthorize); <-- crashes when I F6 (Eclipse) on this line

...然后我注意到我的 xml 布局文件中有一个警告图标:“这个 TableLayout 没用(没有孩子,没有背景,没有 id)”

好吧,我要在运行时添加孩子;为什么需要背景?还有一个大问题:为什么它说它没有 id,当我添加了一个时:

<TableLayout>
android:id="@id/tlDynamicRows" <-- IT'S RIGHT THERE!!!
android:layout_width="match_parent"
android:layout_height="match_parent" > 
</TableLayout>

还有另一个警告说,“这个 TableRow 或其 TableRow 父级是无用的”

这是整个 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/tlDynamic"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >

        <TableRow> <- This is where it says, "This TableRow or its TableRow parent is useless"

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_margin="2dip"
                android:text="@string/demand"
                android:textAppearance="?android:attr/textAppearanceSmall" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_margin="2dip"
                android:text="@string/time"
                android:textAppearance="?android:attr/textAppearanceSmall" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_margin="2dip"
                android:text="@string/space"
                android:textAppearance="?android:attr/textAppearanceSmall" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_margin="2dip"
                android:text="@string/contact"
                android:textAppearance="?android:attr/textAppearanceMedium" />
        </TableRow>
    </TableLayout>

    <ScrollView
        android:id="@+id/scrollViewHost"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fillViewport="true" >

        <TableLayout> <- This is where it says, "says, "This TableRow or its TableRow parent is useless"
        android:id="@id/tlDynamicRows"
        android:layout_width="match_parent"
        android:layout_height="match_parent" > 
        </TableLayout>
    </ScrollView>

</LinearLayout>

是什么导致了这种令人反感的发展?

更新:

查看我的帖子时,我看到 xml 中最后的“TableLayout”有一个多余的直角括号。所以,我取出第一个,现在我得到一个错误消息,即“找不到与给定名称匹配的资源(在 'id' 处,值为 '@id/tlDynamicRows')。”

xml的那部分现在是:

<TableLayout
    android:id="@id/tlDynamicRows"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >
</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/tlDynamic"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >

        <TableRow
            android:layout_width="match_parent"
            android:layout_height="wrap_content" >

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_margin="2dip"
                android:text="@string/demand"
                android:textAppearance="?android:attr/textAppearanceSmall" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_margin="2dip"
                android:text="@string/time"
                android:textAppearance="?android:attr/textAppearanceSmall" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_margin="2dip"
                android:text="@string/space"
                android:textAppearance="?android:attr/textAppearanceSmall" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_margin="2dip"
                android:text="@string/contact"
                android:textAppearance="?android:attr/textAppearanceMedium" />
        </TableRow>
    </TableLayout>

    <ScrollView
        android:id="@+id/scrollViewHost"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fillViewport="true" >

        <TableLayout
            android:id="@+id/tlDynamicRows"
            android:layout_width="match_parent"
            android:layout_height="match_parent" >
        </TableLayout>
    </ScrollView>

</LinearLayout>

我仍然有一条警告消息要在 ScrollView 内的 TableLayout 中更改此行:

android:layout_height="wrap_content"

到:

android:layout_height="wrap_content"

我做了,它似乎没有任何区别 - 但它摆脱了警告,所以我会留下它。

【问题讨论】:

    标签: android android-layout android-xml android-view android-tablelayout


    【解决方案1】:

    您在上面发布的 XML 中有几点需要注意:

    1- 当您使用第一个表格行时

      <TableRow> <- This is where it says, "This TableRow or its TableRow parent is useless"
    

    在这一行中,您没有指定行的 layout_height 和 layout_width 属性,这就是为什么您在警告中得到“无用”的原因。

    2- 另一点是尝试在底部的滚动视图中为表格布局提供 id:

     android:id="@+id/tlDynamicRows"
    

    而不是

     android:id="@id/tlDynamicRows"
    

    希望这些对你有用。

    【讨论】:

    • 我看到的所有例子都有“+”删除所有这些给我的错误消息现在包含三个元素,而不仅仅是一个,例如这个:android:id="@id/scrollViewHost" 给出这个:错误:错误:找不到与给定名称匹配的资源(在 'id' 处,值为 '@id/scrollViewHost')。 ...@id/tlDynamic 和 @id/tlDynamicRows 也是如此。所以我加回了“+”,并且在包含 TableRow 的 layout_width 和 layout_height 属性之后,它终于可以工作了。我在上面添加了我的新 xml 以供后代使用。
    • 我仍然得到,“这个 TableRor 布局或 TableLayout 父级是无用的”,但它正在做我想要它做的事情......
    • @ClayShannon ..此链接可能会帮助您了解表格布局...看看...developer.android.com/reference/android/widget/TableLayout.html
    【解决方案2】:
    猜你喜欢
    • 1970-01-01
    • 2014-06-24
    • 1970-01-01
    • 2016-02-18
    • 1970-01-01
    • 1970-01-01
    • 2017-09-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多