【问题标题】:Android TableLayout not showing all 11 TableRowsAndroid TableLayout 未显示所有 11 个 TableRows
【发布时间】:2014-09-25 12:30:07
【问题描述】:

我正在尝试为 MasterMind 游戏的简单版本创建布局。我已经设置了一个 TableLayout,其中包含 10 个具有相同垂直权重的 TableRows 来表示钉子,并在底部设置了一个更大的最后一行。主要的 10 行中的每一行都包含 4 个 Button 元素(重量相同)和侧面的两个视图,以便按钮保持居中。我已经有了一个我觉得应该可以工作的布局,但目前,只有 9 行会显示(其他行存在,就在屏幕外)。

谁能帮我弄清楚为什么不是所有的元素都显示?我会做一系列线性布局,但我听说效率不高,虽然我不确定我目前的方法是否也是。

我希望得到类似的东西:

但我得到了这个:

这是布局(对不起,它很长/重复):

<?xml version="1.0" encoding="utf-8"?>

<TableLayout
    android:id="@+id/game_layout"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:background="#b3b3b3"
    android:orientation="vertical">

    <TableRow
        android:layout_width="fill_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:orientation="horizontal">

        <View
            android:layout_height="fill_parent"
            android:layout_width="0dp"
            android:layout_weight="2"/>

        <Button
            android:layout_height="fill_parent"
            android:layout_width="0dp"
            android:layout_weight="1"/>
        <Button
            android:layout_height="fill_parent"
            android:layout_width="0dp"
            android:layout_weight="1"/>
        <Button
            android:layout_height="fill_parent"
            android:layout_width="0dp"
            android:layout_weight="1"/>
        <Button
            android:layout_height="fill_parent"
            android:layout_width="0dp"
            android:layout_weight="1"/>

        <View
            android:layout_height="fill_parent"
            android:layout_width="0dp"
            android:layout_weight="2"/>

    </TableRow>

    <TableRow
        android:layout_width="fill_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:orientation="horizontal">

        <View
            android:layout_height="fill_parent"
            android:layout_width="0dp"
            android:layout_weight="2"/>

        <Button
            android:layout_height="fill_parent"
            android:layout_width="0dp"
            android:layout_weight="1"/>
        <Button
            android:layout_height="fill_parent"
            android:layout_width="0dp"
            android:layout_weight="1"/>
        <Button
            android:layout_height="fill_parent"
            android:layout_width="0dp"
            android:layout_weight="1"/>
        <Button
            android:layout_height="fill_parent"
            android:layout_width="0dp"
            android:layout_weight="1"/>

        <View
            android:layout_height="fill_parent"
            android:layout_width="0dp"
            android:layout_weight="2"/>

    </TableRow>

    <TableRow
        android:layout_width="fill_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:orientation="horizontal">

        <View
            android:layout_height="fill_parent"
            android:layout_width="0dp"
            android:layout_weight="2"/>

        <Button
            android:layout_height="fill_parent"
            android:layout_width="0dp"
            android:layout_weight="1"/>
        <Button
            android:layout_height="fill_parent"
            android:layout_width="0dp"
            android:layout_weight="1"/>
        <Button
            android:layout_height="fill_parent"
            android:layout_width="0dp"
            android:layout_weight="1"/>
        <Button
            android:layout_height="fill_parent"
            android:layout_width="0dp"
            android:layout_weight="1"/>

        <View
            android:layout_height="fill_parent"
            android:layout_width="0dp"
            android:layout_weight="2"/>

    </TableRow>

    <TableRow
        android:layout_width="fill_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:orientation="horizontal">

        <View
            android:layout_height="fill_parent"
            android:layout_width="0dp"
            android:layout_weight="2"/>

        <Button
            android:layout_height="fill_parent"
            android:layout_width="0dp"
            android:layout_weight="1"/>
        <Button
            android:layout_height="fill_parent"
            android:layout_width="0dp"
            android:layout_weight="1"/>
        <Button
            android:layout_height="fill_parent"
            android:layout_width="0dp"
            android:layout_weight="1"/>
        <Button
            android:layout_height="fill_parent"
            android:layout_width="0dp"
            android:layout_weight="1"/>

        <View
            android:layout_height="fill_parent"
            android:layout_width="0dp"
            android:layout_weight="2"/>

    </TableRow>

    <TableRow
        android:layout_width="fill_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:orientation="horizontal">

        <View
            android:layout_height="fill_parent"
            android:layout_width="0dp"
            android:layout_weight="2"/>

        <Button
            android:layout_height="fill_parent"
            android:layout_width="0dp"
            android:layout_weight="1"/>
        <Button
            android:layout_height="fill_parent"
            android:layout_width="0dp"
            android:layout_weight="1"/>
        <Button
            android:layout_height="fill_parent"
            android:layout_width="0dp"
            android:layout_weight="1"/>
        <Button
            android:layout_height="fill_parent"
            android:layout_width="0dp"
            android:layout_weight="1"/>

        <View
            android:layout_height="fill_parent"
            android:layout_width="0dp"
            android:layout_weight="2"/>

    </TableRow>

    <TableRow
        android:layout_width="fill_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:orientation="horizontal">

        <View
            android:layout_height="fill_parent"
            android:layout_width="0dp"
            android:layout_weight="2"/>

        <Button
            android:layout_height="fill_parent"
            android:layout_width="0dp"
            android:layout_weight="1"/>
        <Button
            android:layout_height="fill_parent"
            android:layout_width="0dp"
            android:layout_weight="1"/>
        <Button
            android:layout_height="fill_parent"
            android:layout_width="0dp"
            android:layout_weight="1"/>
        <Button
            android:layout_height="fill_parent"
            android:layout_width="0dp"
            android:layout_weight="1"/>

        <View
            android:layout_height="fill_parent"
            android:layout_width="0dp"
            android:layout_weight="2"/>

    </TableRow>

    <TableRow
        android:layout_width="fill_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:orientation="horizontal">

        <View
            android:layout_height="fill_parent"
            android:layout_width="0dp"
            android:layout_weight="2"/>

        <Button
            android:layout_height="fill_parent"
            android:layout_width="0dp"
            android:layout_weight="1"/>
        <Button
            android:layout_height="fill_parent"
            android:layout_width="0dp"
            android:layout_weight="1"/>
        <Button
            android:layout_height="fill_parent"
            android:layout_width="0dp"
            android:layout_weight="1"/>
        <Button
            android:layout_height="fill_parent"
            android:layout_width="0dp"
            android:layout_weight="1"/>

        <View
            android:layout_height="fill_parent"
            android:layout_width="0dp"
            android:layout_weight="2"/>

    </TableRow>

    <TableRow
        android:layout_width="fill_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:orientation="horizontal">

        <View
            android:layout_height="fill_parent"
            android:layout_width="0dp"
            android:layout_weight="2"/>

        <Button
            android:layout_height="fill_parent"
            android:layout_width="0dp"
            android:layout_weight="1"/>
        <Button
            android:layout_height="fill_parent"
            android:layout_width="0dp"
            android:layout_weight="1"/>
        <Button
            android:layout_height="fill_parent"
            android:layout_width="0dp"
            android:layout_weight="1"/>
        <Button
            android:layout_height="fill_parent"
            android:layout_width="0dp"
            android:layout_weight="1"/>

        <View
            android:layout_height="fill_parent"
            android:layout_width="0dp"
            android:layout_weight="2"/>

    </TableRow>

    <TableRow
        android:layout_width="fill_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:orientation="horizontal">

        <View
            android:layout_height="fill_parent"
            android:layout_width="0dp"
            android:layout_weight="2"/>

        <Button
            android:layout_height="fill_parent"
            android:layout_width="0dp"
            android:layout_weight="1"/>
        <Button
            android:layout_height="fill_parent"
            android:layout_width="0dp"
            android:layout_weight="1"/>
        <Button
            android:layout_height="fill_parent"
            android:layout_width="0dp"
            android:layout_weight="1"/>
        <Button
            android:layout_height="fill_parent"
            android:layout_width="0dp"
            android:layout_weight="1"/>

        <View
            android:layout_height="fill_parent"
            android:layout_width="0dp"
            android:layout_weight="2"/>

    </TableRow>

    <TableRow
        android:layout_width="fill_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:orientation="horizontal">

        <View
            android:layout_height="fill_parent"
            android:layout_width="0dp"
            android:layout_weight="2"/>

        <Button
            android:layout_height="fill_parent"
            android:layout_width="0dp"
            android:layout_weight="1"/>
        <Button
            android:layout_height="fill_parent"
            android:layout_width="0dp"
            android:layout_weight="1"/>
        <Button
            android:layout_height="fill_parent"
            android:layout_width="0dp"
            android:layout_weight="1"/>
        <Button
            android:layout_height="fill_parent"
            android:layout_width="0dp"
            android:layout_weight="1"/>

        <View
            android:layout_height="fill_parent"
            android:layout_width="0dp"
            android:layout_weight="2"/>

    </TableRow>

    <TableRow
        android:layout_width="fill_parent"
        android:layout_height="0dp"
        android:layout_weight="2"
        android:orientation="horizontal">

        <View
            android:layout_height="fill_parent"
            android:layout_width="0dp"
            android:layout_weight="2"/>

    </TableRow>

</TableLayout>

【问题讨论】:

  • 把它放在滚动视图中..

标签: android xml tablelayout tablerow


【解决方案1】:

把整个东西包起来:

<ScrollView
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:scrollbars="none"
    android:layout_weight="1">
   <TableLayout
    android:id="@+id/game_layout"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:background="#b3b3b3"
    android:orientation="vertical">
    ...

</ScrollView>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-06-25
    • 1970-01-01
    • 1970-01-01
    • 2014-01-30
    • 1970-01-01
    • 1970-01-01
    • 2021-10-10
    相关资源
    最近更新 更多