这是我编写的示例 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/