Android:创建多列按钮,每列都有独立的 ScrollView

标签 android scroll multiple-columns

这是我编写的示例 XML。我正在尝试制作一个垂直对齐的按钮列表,以及另外两列。所以总共会有 3 列。每列都可以垂直滚动。如何添加更多列并使它们单独滚动?

我搜索并尝试了其他几种变体;我能得到的最接近的是表格布局,但垂直滚动似乎不可能!

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

<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
                android:id="@+id/table" 
                android:layout_width="fill_parent"
                android:layout_height="wrap_content" 
                android:stretchColumns="*"> 

                <Button 
                    android:id="@+id/button1"
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:text="@string/button1"/>

                <Button
                    android:id="@+id/button2"
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:text="@string/button2"/>
                <Button
                     android:id="@+id/button3"
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:text="@string/button3"/>

</TableLayout>

最佳答案

您可以使用一个水平 LinearLayout 作为根容器,然后为具有相同宽度并包含垂直 LinearLayout 的每一列使用一个 ScrollView:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >

<ScrollView
    android:layout_width="0dp"
    android:layout_height="match_parent"
    android:layout_weight="1" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical" >

        <Button
            android:id="@+id/button1_1"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="@string/button1" />

        <Button
            android:id="@+id/button1_2"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="@string/button1" />

        <Button
            android:id="@+id/button1_3"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="@string/button1" />
    </LinearLayout>
</ScrollView>

<ScrollView
    android:layout_width="0dp"
    android:layout_height="match_parent"
    android:layout_weight="1" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical" >

        <Button
            android:id="@+id/button2_1"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="@string/button1" />

        <Button
            android:id="@+id/button2_2"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="@string/button1" />

        <Button
            android:id="@+id/button2_3"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="@string/button1" />
    </LinearLayout>
</ScrollView>

<ScrollView
    android:layout_width="0dp"
    android:layout_height="match_parent"
    android:layout_weight="1" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical" >

        <Button
            android:id="@+id/button3_1"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="@string/button1" />

        <Button
            android:id="@+id/button3_2"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="@string/button1" />

        <Button
            android:id="@+id/button3_3"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="@string/button1" />
    </LinearLayout>
</ScrollView>

关于Android:创建多列按钮,每列都有独立的 ScrollView ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14395848/

相关文章:

android - 指定gradle文件(不是默认的build.gradle文件)来构建android studio标准项目

android - 触发系统音量条

android - 带有 Robolectric 和 Flavors 的 Resources$NotFoundException

javascript - 在页面滚动时显示/隐藏导航菜单的偏移量

javascript - 在 Cordova 中禁用硬件后退按钮

Angular 7 - 虚拟滚动与异步订阅相结合

javascript - 导航 div 滚动条

html - CSS3 : Multi Column System with Titles

c# - 在 VS 2010 C# .NET Windows 窗体中重现 Microsoft 注册表编辑器 "Edit Binary Value"窗口

Matlab:如果满足逻辑条件,如何将列号写入新列