2012-10-15 263 views
4

我是Android開發的初學者,我正嘗試創建一個包含不同佈局的水平滾動視圖。android水平滾動視圖

我面對的錯誤是:horozontal滾動視圖只能託管一個直接的孩子。 請提前

<HorizontalScrollView xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tool" 
     android:id="@+id/horizontalScrollView1" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:fillViewport="true" > 

     <LinearLayout 
      android:layout_width="wrap_content" 
      android:layout_height="fill_parent" 
      android:orientation="horizontal" 
      android:background="#ff0000"> 

      <LinearLayout 
       android:layout_width="wrap_content" 
       android:layout_height="fill_parent" 
       android:orientation="horizontal" 
       android:background="#ff0000"> 

      </LinearLayout> 

      <LinearLayout 
       android:layout_width="wrap_content" 
       android:layout_height="fill_parent" 
       android:orientation="horizontal" 
       android:background="#00ff00"> 

      </LinearLayout> 

     </LinearLayout> 

    </HorizontalScrollView> 
+0

你能更精確地定義你的問題嗎?你的意思是不同的佈局? –

+0

我會將每個佈局視爲一個視圖 –

+0

關鍵是這個佈局沒有任何問題。一切都被恰當地封裝。一切都在一個LinearLayout中,因爲它應該在Horizo​​ntalScrollView中。無論您在一個LinearLayout中放入多少東西,它們都會向左/向右滾動。 –

回答

4

不僅水平,但任何垂直滾動視圖也會給這個錯誤建議的感謝。 這個錯誤意味着應該只有一個孩子進入scrollview,並且該孩子可以包含任意數量的子孩子。

因此,底線是thios您應該只有一個直接的孩子滾動視圖,並在該子佈局只能作爲

<HorizontalScrollView xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tool" 
    android:id="@+id/horizontalScrollView1" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:fillViewport="true" > 

    <LinearLayout 

     android:id="@+id/directchild" 
     android:layout_width="wrap_content" 
     android:layout_height="fill_parent" 
     android:orientation="horizontal" 
     android:background="#ff0000"> 

    </LinearLayout> 

</HorizontalScrollView> 

現在directchild內創建所需的佈局layout.You不會得到任何錯誤那麼

+0

其他佈局已經是這個主佈局的子元素,而不是滾動視圖的子元素 –

+0

這不會在我的eclipse上顯示任何錯誤。如果要查看效果,請給出不同佈局的寬度併爲主佈局指定fillparent寬度,你會看到滾動效果。 – Saurabh