2013-01-18 57 views
1

這是我寫的一個示例XML。我正在嘗試製作一個垂直對齊的按鈕列表,還有2個這樣的列。所以總共會有3列。每列都可以垂直滾動。你如何添加更多的列並讓它們單獨滾動?Android:創建多列按鈕,每個按鈕都有獨立的滾動視圖

我搜索並嘗試了其他幾種變化;我能找到的最接近桌面佈局,但是垂直滾動似乎不可能!

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

你可以使用'ListView's,但我認爲並排多個列表對於小屏幕來說不是一個好主意。 –

回答

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> 

+0

這將做我猜!!!!! – IronBlossom

+0

太棒了!謝謝,幫助!你能分享一個很好的鏈接來解釋可接受的親子關係與佈局以及佈局如何工作嗎?早些時候,我試圖在相對佈局中有一個表佈局,但是這使得父佈局(相對)無用/無效。 – sabergeek

+0

http://developer.android.com/guide/topics/ui/declaring-layout.html :) – fiddler

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

Yo你可以通過創建一個水平的線性佈局並且放置三個列表視圖並排放置它們的高度作爲填充父寬度和每個總寬度的1/3。現在您可以分別在每個列表中放置按鈕,並可以單獨滾動它們。你可以嘗試這種方法,看看它是否是你想要的。

相關問題