【问题标题】:Android LinearLayout in HorizontalScrollView with multiple rows具有多行的Horizo​​ntalScrollView中的Android LinearLayout
【发布时间】:2014-06-05 19:54:20
【问题描述】:

我在 Horizo​​ntalScrollView 中使用 LinearLayout,滚动部分正在工作,但我不知道如何制作 3 行。


例如:

粗体显示当前显示的内容(在模拟器中/屏幕上)

当前

--Button1--Button2--Button3-- Button4--Button5--Button6--Button7--Button8--Button9--Button10

-Button11--Button12

我想要什么

--Button1--Button2--Button3-- Button4--Button5--Button6--
--Button7--Button8--Button9-- Button10--Button11--Button12--


我正在尝试使用一个 LinearView 来做到这一点,因为稍后我将尝试动态添加按钮。

我可能以完全错误的方式做这件事(我想我是)。

代码如下:

 <HorizontalScrollView
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:layout_marginTop="50dp" >

    <LinearLayout
        android:layout_width="200dp"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

            <Button
            android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Button1" />

            <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Button2" />

            <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Button3" />

            <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Button4" />

            <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Button5" />
             <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Button6" />
             <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Button7" />
             <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Button8" />
              <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Button9" />
               <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Button10" />
                <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Button11" />
                 <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Button12" />

        </LinearLayout>
    </HorizontalScrollView>

我尝试了一些事情,但我总是回到起点。

【问题讨论】:

标签: android android-linearlayout horizontalscrollview


【解决方案1】:

代替 LinearLayout 试试 GridLayout,它是 Android 支持库的一部分。

在XML布局中实现时可以设置列数和行数。

类似下面的东西

 <HorizontalScrollView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="50dp" >

    <GridLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:columnCount="6"
        android:rowCount="3"
        android:orientation="horizontal" >

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Button1" />

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Button2" />

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Button3" />

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Button4" />

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Button5" />

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Button6" />

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Button7" />

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Button8" />

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Button9" />

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Button10" />

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Button11" />

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Button12" />
    </GridLayout>
 </HorizontalScrollView>

编辑 - 如果要添加不同宽度的子视图,可以使用 TableLayout 而不是 GridLayout,如下所示

  <HorizontalScrollView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="50dp" >

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

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

            <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Button1" />

            <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Button2" />

            <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Button3" />

            <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Button4" />

            <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Button5" />

            <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Button6" />
        </TableRow>

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

            <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Button7" />

            <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Button8" />

            <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Button9" />

            <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Button10" />

            <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Button11" />

            <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Button12" />
        </TableRow>
    </TableLayout>
</HorizontalScrollView>

【讨论】:

  • 这么简单的解决方案,谢谢,我完全看错了。但我有另一个问题,当我这样做时,一些按钮比其他按钮大,其中一些按钮之间有空白。我想说的并不是所有的尺寸都一样。
  • 您可以使用带有 数量的 TableLayout 而不是 GridLayout 来显示这种网格。检查编辑部分