2011-04-24 166 views
13

有隻允許垂直滾動的ScrollView和只允許水平滾動的Horizo​​ntalScrollView,但兩者都沒有類。這看起來像是Android UI中的一個非常大的缺陷。任何技巧,讓這個?Android - 如何允許水平和垂直滾動

+2

在很多情況下,任何需要大量空間的東西都不應該使用ScrollView/Horizo​​ntalScrollView。只有當內容在內存消耗方面仍然相當適中時,這些纔有用。在頻譜的另一端,考慮谷歌地圖 - 這不是使用ScrollView或Horizo​​ntalScrollView實現的,因爲我們無法一次在RAM中保存整個地球的地圖。想一想你想要滾動的內容,因爲很可能你需要做一些比使用'ScrollView'和親族來處理移動設備更復雜的東西。 – CommonsWare 2011-04-24 15:44:08

+0

我的解決方案是計算哪個關卡(這是一款遊戲)的物體在屏幕上,並且只繪製這些物體。另一種解決方案是使用父寬度/高度的框架視圖,並在事件發生時移動關卡的對象。後者似乎需要更多的處理,但可能更少的RAM。你有推薦嗎? – 2011-04-24 15:50:55

+1

「我的解決方案是計算哪個關卡(這是一款遊戲)的物體在屏幕上,並且只繪製這些關卡。」 - 我不是遊戲開發者,但是這聽起來像你不需要'ScrollView'和親戚,然後,你自己正在處理它。只需注意滑動事件並重新繪製您的關卡。 – CommonsWare 2011-04-24 16:19:42

回答

28

試試這個

<?xml version="1.0" encoding="utf-8"?> 
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android" 
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content"> 

    <HorizontalScrollView xmlns:android="http://schemas.android.com/apk/res/android" 
        android:layout_width="wrap_content" 
        android:layout_height="fill_parent"> 

     <TableLayout 
        android:id="@+id/amortization" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content"> 

       <TableRow 
        android:background="#ffff00"> 
        <TextView 
         android:text="@string/amortization_1" 
         android:padding="3dip"/> 
        <TextView 
         android:text="@string/amortization_2" 
         android:padding="3dip"/> 
        <TextView 
         android:text="@string/amortization_3" 
         android:padding="3dip"/> 
        <TextView 
         android:text="@string/amortization_4" 
         android:padding="3dip"/> 
        <TextView 
         android:text="@string/amortization_5" 
         android:padding="3dip"/> 
        <TextView 
         android:text="@string/amortization_6" 
         android:padding="3dip"/> 
        <TextView 
         android:text="@string/amortization_7" 
         android:padding="3dip"/> 
       </TableRow> 
     </TableLayout> 
    </HorizontalScrollView> 
</ScrollView> 
+0

在FrameLayout上進行翻譯是最好的,methinks – 2011-04-28 21:23:15

+0

這可以正常工作,但一次只能滾動水平或垂直,而不能對角滾動。感覺未完成,但易於實施。 – Hersker 2013-05-08 11:57:03

+0

我有一個很大的數字網格,我必須顯示,並且完美地工作。雖然你不能對角滾動,但在像我這樣的情況下這並不是什麼大事。 – DiscDev 2013-05-09 21:14:51

2

與ImageView的一個例子:

<?xml version="1.0" encoding="utf-8"?> 
<ScrollView android:id="@+id/ScrollView02" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      xmlns:android="http://schemas.android.com/apk/res/android"> 
<HorizontalScrollView android:id="@+id/HorizontalScrollView01" 
         android:layout_width="wrap_content" 
         android:layout_height="wrap_content"> 
<ImageView android:id="@+id/ImageView01" 
      android:src="@drawable/pic" 
      android:isScrollContainer="true" 
      android:layout_height="fill_parent" 
      android:layout_width="fill_parent" 
      android:adjustViewBounds="true"> 
</ImageView> 
</HorizontalScrollView> 
</ScrollView> 

來源:http://www.android-spa.com/viewtopic.php?t=3959&highlight=scrollview+vertical+horizontal

+2

恕我直言,一個需要兩個滾動的屏幕根本不舒服(實際上我認爲垂直滾動不是一個好主意),但它起作用。 – 2011-04-24 15:42:16

+0

這是一個比屏幕大得多的遊戲。哥們,謝啦。 – 2011-04-24 15:43:50

1

我發現設置fillViewport是很重要的,否則滾動條可能會出現在隨機位置而不是在滾動區域的右側/底部:

<ScrollView 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:fillViewport="true" > 
    <HorizontalScrollView 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:fillViewport="true" > 
    </HorizontalScrollView> 
</ScrollView>