2011-10-19 35 views
0

我需要一個充滿圖像作爲背景和三個按鈕浮動在圖像上的屏幕。我創建了這個很好的XML和垂直方向它做我想要的。然而,在景觀中,它不,所以我試圖添加一個滾動視圖,並且對如何正確地組合這些元素感到非常困惑。各種嘗試後我到達圖所示滾動背景圖片和浮動按鈕

<?xml version="1.0" encoding="utf-8"?> 
<ScrollView android:layout_height="fill_parent" 
android:layout_width="fill_parent" android:id="@+id/home_container" 
xmlns:android="http://schemas.android.com/apk/res/android"> 
<ImageView android:src="@drawable/alert" 
    android:layout_height="fill_parent" android:layout_width="fill_parent" 
    android:scaleType="fitCenter" android:clickable="true" 
    android:enabled="true" /> 
<LinearLayout android:layout_width="fill_parent" 
    android:paddingBottom="25px" android:paddingLeft="25px" 
    android:orientation="vertical" android:paddingTop="90px"  android:id="@+id/toplinear" 
    android:layout_height="fill_parent" android:paddingRight="25px" 
    android:gravity="top"> 
    <Button android:layout_width="fill_parent" android:textSize="20dp" 
     android:id="@+id/startprogrambutton" android:paddingBottom="12dp" 
     android:text="@string/text1" android:layout_height="wrap_content" 
     android:paddingTop="12dp"></Button> 
    <Button android:layout_width="fill_parent" android:textSize="20dp" 
     android:id="@+id/button1" android:paddingBottom="12dp" 
     android:layout_marginTop="10dp" android:background="@drawable/webview_buttons" 
     android:text="@string/text2" android:layout_height="wrap_content" 
     android:paddingTop="12dp"></Button> 
    <Button android:layout_width="fill_parent" android:textSize="20dp" 
     android:id="@+id/button2" android:paddingBottom="12dp" 
     android:layout_marginTop="10dp" android:background="@drawable/webview_buttons" 
     android:text="@string/text3" android:layout_height="wrap_content" 
     android:paddingTop="12dp"></Button> 
    <Button android:layout_width="fill_parent" android:textSize="20dp" 
     android:id="@+id/button3" android:paddingBottom="12dp" 
     android:layout_marginTop="10dp" android:background="@drawable/webview_buttons" 
     android:text="@string/text4" android:layout_height="wrap_content" 
     android:paddingTop="12dp"></Button> 
</LinearLayout> 

舞臺現在與滾動視圖的錯誤只能包含一個孩子。有什麼方法可以重新排列,以便我可以將圖像和浮動按鈕全部滾動到一起?

回答

0

ImageViewLinearLayout都放在另一垂直LinearLayout中。這解決了您的scrollView子問題。

<ScrollView> 
<LinearLayout> 
<Imageview /> 
<LinearLayout /> 
</LinearLayout> 
</ScrollView> 

爲什麼不給圖像作爲linearLayout的按鈕的背景,它將爲您的目的服務。

+0

即停止它errorring但現在的圖像不是背景,而是上面的扣子一個單獨的元素。我忘了提及,當它作爲非滾動工作正常時,我有一切在框架佈局。據我瞭解,scrollview是一個框架佈局,我認爲它會替換它。 – ron

+0

使用android:background =「@ drawable/alert」屬性進行帶按鈕的線性佈局。刪除imageView。 ,是的,你可以用frameLayout替換它。將該frameLayout放置在滾動視圖中。 –

+0

謝謝Yashwanth - 背景attr didi它。 – ron

0

如果要滾動只是你的按鈕,而不是ImageView的你可以試試這個:

<LinearLayout> 
    <Imageview /> 
    <ScrollView> 
     <LinearLayout> 
      <Button /> 
      <Button /> 
      <Button /> 
      <Button /> 
     </LinearLayout> 
    </ScrollView> 
</LinearLayout> 

希望這有助於!

0

就這麼你知道,你並不侷限於試圖爲兩個方向找到一個解決方案。通過在land &端口目錄中指定單獨的佈局xml文件,您可以爲兩個方向設置2個單獨的佈局。看到這裏的官方文檔:

http://developer.android.com/guide/practices/screens_support.html

+1

感謝您對本文有幫助的參考 – ron

+0

NP ron ...如何展示一些熱愛w/upvote? :O) – SBerg413