2013-03-16 96 views
0

我是Android新手(用於使用Visual Studio進行開發...),至今我對佈局有疑問:Android:頂級菜單欄+滾動視圖?

我想要做的是在頂部有一個酒吧的佈局(只是放置一些按鈕或額外信息)以及填充屏幕其餘部分的滾動視圖。 這是我做它到目前爲止的方式:

<RelativeLayout 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" 
tools:context=".Home" > 

<LinearLayout 
    android:id="@+id/linearLayout1" 
    android:layout_width="match_parent" 
    android:layout_height="50dp" 
    android:layout_alignParentLeft="true" 
    android:layout_alignParentTop="true" 
    android:background="@android:color/white" > 

    <!-- I can put any button or text I want in here --> 

</LinearLayout> 

<ScrollView 
    android:id="@+id/scrollView1" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:layout_marginTop="50dp" 

    android:background="@drawable/wallpaper" > 


</ScrollView> 

這是工作如我所料,但我的問題是,如果我做了正確的方式還是應該做一個更好的方式(最有效)。

在此先感謝

+0

在ScrollView中刪除'android:layout_marginTop =「50dp」'並添加'android:layout_below =「 @ id/linearLayout1「'除此之外你做得很好。 – VenomVendor 2013-03-16 09:44:32

回答

1

不推薦絕對定位。例如,如果您需要將高度從50dp增加到100dp,則必須在幾個不同的位置更改此值。

我知道至少有兩種方法來改善您的佈局。

1)使用的RelativeLayout(android:layout_below="@id/linearLayout1"或其對應的layout_above特徵)

<RelativeLayout ...> 
    <LinearLayout 
     android:id="@+id/linearLayout1" 
     android:layout_width="match_parent" 
     android:layout_height="50dp" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentTop="true" 
     android:background="@android:color/white"> 
    </LinearLayout> 

    <ScrollView 
     android:id="@+id/scrollView1" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_below="@id/linearLayout1" 
     android:fillViewport="true" 
     android:background="@drawable/wallpaper" > 
    </ScrollView> 
</RelativeLayout> 

2)的LinearLayout更換(和使用android:layout_weight="1.0"

<LinearLayout ...> 
    <LinearLayout 
     android:id="@+id/linearLayout1" 
     android:layout_width="match_parent" 
     android:layout_height="50dp" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentTop="true" 
     android:background="@android:color/white"> 
    </LinearLayout> 

    <ScrollView 
     android:id="@+id/scrollView1" 
     android:layout_width="match_parent" 
     android:layout_height="0dip" 
     android:layout_weight="1.0" 
     android:fillViewport="true" 
     android:background="@drawable/wallpaper" > 
    </ScrollView> 
</LinearLayout> 

android:layout_height="0dip"可以看起來很奇怪。實際上,您可以使用match_parent,但Eclipse IDE會突出顯示此行,並且如果您指定android:layout_weight,建議使用0dip

此外,我添加了android:fillViewport="true"到您的滾動視圖。它表明滾動視圖內的內容將擴展到全高,如果有必要,你可以閱讀關於此屬性here