2016-03-22 28 views
-1

我想要在頂部(包含按鈕)對齊一個水平滾動條,然後在此之後(其中可以包含我放入的任何東西)然後最後一個水平滾動條滾動條在底部對齊。如何強制將水平滾動條固定到頂部/底部,然後將中間展開的相對佈局與之間的其他所有元素進行比較?如何在Android中安排這三個項目

+0

您可以發佈小號creenshot或線框或代碼 –

+0

您可以使用此佈局的分區lik使用layout_weight =「」 –

+0

我希望頂部和底部具有包裹其內容的高度,但是中心相對佈局覆蓋了所有介於 –

回答

0

使用的LinearLayout ...

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
       android:orientation="vertical" 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" 
       android:gravity="center"> 
    <RelativeLayout 
     android:id="@+id/top_container" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content"></RelativeLayout> 
    <RelativeLayout 
     android:id="@+id/middle_container" 
     android:layout_width="match_parent" 
     android:layout_height="0dp" 
     android:layout_weight="1"></RelativeLayout> 
    <RelativeLayout 
     android:id="@+id/bottom_container" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content"> 
    </RelativeLayout> 
</LinearLayout> 

使用的RelativeLayout ....

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" 
    > 
    <RelativeLayout 
     android:id="@+id/top_container" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_alignParentTop="true"></RelativeLayout> 
    <RelativeLayout 
     android:id="@+id/bottom_container" 
     android:layout_width="match_parent" 
     android:layout_alignParentBottom="true" 
     android:layout_height="wrap_content"> 
    </RelativeLayout> 
    <RelativeLayout 
     android:id="@+id/middle_container" 
     android:layout_width="match_parent" 
     android:layout_below="@id/top_container" 
     android:layout_above="@id/bottom_container" 
     android:layout_height="match_parent"></RelativeLayout> 
</RelativeLayout> 
+0

之間的所有內容'android:layout_height =「0dp」/ android:layout_weight =「1」'是我一直想念的。這與使用match_parent或wrap_content有什麼不同?這是如何起作用的? –

+0

好吧,所以你的頂部容器和底部容器高度是wrap_content,它只會取得他們孩子所需的高度,並且通過在中間容器中指定weight = 1和height = 0dp,它將佔用剩餘的高度。 – Ritesh

+0

通過將屬性設置爲width =「0dp」和weight =「1」,您可以在寬度上實現同樣的效果。 – Ritesh

0

可以使用layout_weight一個LinearLayout內有它填補了佈局擴張。

例如:

<LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:orientation="vertical" > 
    <HorizontalScrollView 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" > 
     <!-- Buttons --> 
    </HorizontalScrollView> 
    <RelativeLayout 
      android:layout_width="match_parent" 
      android:layout_height="0dp" 
      android:layout_weight="1" > 
     <!-- Content --> 
    </RelativeLayout> 
    <HorizontalScrollView 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" > 
     <!-- More Buttons --> 
    </HorizontalScrollView> 
</LinearLayout> 
0

安置自己的線框,以便我們能夠更好的幫助。不過我認爲你的解決方案可能是: - 保持你的主佈局爲RelativeLayout。在你的主佈局下以垂直方向取向另一個LinearLayout。在LinearLayout和LinearLayout之外放置水平滾動視圖和其他項目,使用此屬性保持水平滾動視圖:

機器人:layout_alignParentBottom = 「真」

您的XML看起來就像這樣:

<RelativeLayout> 
    <LinearLyout> 
    // horizontal scrollview 
    // other items 
    </LinearLayout> 
    // horizaontal scrollview with alignparentbottom="true" 
</RelativeLAyout>