Android:创建多列按钮,每列都有独立的滚动视图

时间:2013-01-18 09:26:06

标签: android scroll multiple-columns

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

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

<?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>

3 个答案:

答案 0 :(得分:2)

您可以使用一个水平LinearLayout作为根容器,然后对每个列使用ScrollView,其宽度相等且包含垂直LinearLayout

<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>

答案 1 :(得分:0)

您可以通过创建水平线性布局并将三个列表视图并排放置,将其高度作为填充父级,宽度分别为总屏幕宽度的1/3。现在,您可以分别在每个列表中放置按钮,并可以单独滚动它们。您可以尝试这种方法,看看它是否是您想要的。

答案 2 :(得分:-1)

你可以这样做:

<LinearLayout 
    ....
     android:orientation="horizontal">
    <ScrollView ...
       android:layout_weight="1">
      <LinearLayout android:orientation="vertical">
        <Button android:id="@+id/b4">
        <Button android:id="@+id/b5">
        <Button android:id="@+id/b6">
      </LinearLayout>
    </ScrollView>
    <ScrollView ...
       android:layout_weight="1">
      <LinearLayout android:orientation="vertical">
        <Button android:id="@+id/b7">
        <Button android:id="@+id/b8">
        <Button android:id="@+id/b9">
      </LinearLayout>
    </ScrollView>
    <ScrollView ...
       android:layout_weight="1">
      <LinearLayout android:orientation="vertical">
        <Button android:id="@+id/b1">
        <Button android:id="@+id/b2">
        <Button android:id="@+id/b3">
      </LinearLayout>
    </ScrollView>
</LinearLayout>