android - "Exception raised during rendering: column indices (start + span) mustn' t 超出了列数。 “

标签 android layout android-gridlayout

我是 Android 编程的新手,我正在尝试创建一个并排放置三个项目的简单布局。

我正在使用 GridLayout 来尝试完成此操作。我创建了一个包含三列的 GridLayout。然后我向其中添加了三个 NumberPickers。当我这样做时,columnCount 上升到 4。当我试图将它改回 3 时,出现错误

渲染期间出现异常:列索引(开始 + 跨度)不得超过列数。

这很奇怪。为什么列索引超过列数?另外,为什么列在接触屏幕右侧之前就结束了?

Android Screen

XML

<?xml version="1.0" encoding="utf-8"?>
<GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/GridLayout1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:columnCount="2"
    android:orientation="horizontal" >

    <NumberPicker
        android:id="@+id/NumberPicker01"
        android:layout_gravity="left"
        android:gravity="center_vertical" />

    <NumberPicker
        android:id="@+id/NumberPicker02"
        android:layout_column="0"
        android:layout_gravity="fill_horizontal" />

    <NumberPicker
        android:id="@+id/NumberPicker03"
        android:layout_column="0" />

    <Space
        android:layout_column="1"
        android:layout_gravity="fill_vertical"
        android:layout_row="3" />

</GridLayout>

最佳答案

当我使用您的布局 xml 时,它在彼此下方放置了三个数字,即三行。选择器 1 进入第 0 行和第 0 列。对于选择器 2 和 3,您指定第 0 列,以便它们都进入新行。此外,您还可以水平填充,这会将任何内容推到屏幕右侧。为什么你那里有空间?最后,columnCount 为 2。列数应为三。

<GridLayout  xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/GridLayout1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:columnCount="3"
android:orientation="horizontal" >

<NumberPicker
    android:id="@+id/NumberPicker01"
    android:layout_column="0" />

<NumberPicker
    android:id="@+id/NumberPicker02"
    android:layout_column="1"/>

<NumberPicker
    android:id="@+id/NumberPicker03"
    android:layout_column="2" />

</GridLayout>

结果是这样的:

enter image description here

您可以通过添加边距来增加数字选择器之间的空间。

关于android - "Exception raised during rendering: column indices (start + span) mustn' t 超出了列数。 “,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25540858/

相关文章:

java - 使用基于 android 的客户端从 mysql 中获取数据

Android GridLayout - 如何增加列或行之间的间距?

java - GridLayout 在后台运行良好,但在真实设备上不显示

java - Android:TableLayout 不显示

ruby-on-rails - 带有符号的布局会产生错误

java - 按 ID 查找所有 View

Android TextView 右对齐

java - setOnItemClickListener 自定义 Listview

android - 格式化从 ExifInterface.TAG_DATETIME 获取的时间和日期

android - 如何使我自己的小部件在 XML 布局文件中使用