2011-04-20 70 views
0

我瞭解班輪和相對佈局的功能。Android:佈局指導

但是我很困惑選擇哪一個活動,因爲我可以滿足他們兩個的需求。

由於我在模擬器上工作,所以我認爲我錯過了一些東西,是否有任何指導或石蕊何時使用哪種佈局?

回答

2

這是我如何嵌套各種意見。從這個例子中你可以看到我在最底層使用ScrollView,這樣視圖就可以很容易地滾動了。

然後我使用滾動視圖的線性佈局ontop,這樣我就可以在屏幕上逐行堆疊小部件。

最後,我使用RelativeLayout,以便我可以使用「layout_alignParentBottom」參數並使按鈕顯示在視圖的底部。

<?xml version="1.0" encoding="utf-8"?> 

<!-- use ScrollView incase it doesn't fit on small display --> 
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android" 
      android:orientation="vertical" 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent" 
      android:fillViewport="true" 
      android:background="#fffcb95a"> 

    <LinearLayout android:orientation="vertical" 
       android:layout_width="fill_parent" 
       android:layout_height="fill_parent"> 

     <!-- Hello World --> 
     <TextView android:text="Hello World" 
        android:id="@+id/TextViewHeaderMessage1" 
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content" 
        android:paddingTop="8dip" 
        android:textAppearance="?android:attr/textAppearanceLarge" 
        android:gravity="center_horizontal" 
        android:paddingBottom="30dip" 
        android:textColor="#6a7349" /> 



     <RelativeLayout android:orientation="horizontal" 
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content"> 

      <!-- "OK" BUTTON --> 
      <Button android:text="OK" 
        android:id="@+id/ok_button" 
        android:layout_width="150dip" 
        android:layout_height="wrap_content" 
        android:layout_alignParentBottom="true" 
        android:layout_centerHorizontal="true" /> 

     </RelativeLayout> 

    </LinearLayout> 

</ScrollView> 
1

也不是天生優於其他...使用RelativeLayout的,如果你打算如何處理彼此相對定位的對象了很多... RelativeLayouts更加靈活,但需要多一點注意正確對齊...如果您有可以水平或垂直整齊放置的物品,LinearLayout會更容易...如果您的情況適用於您最喜歡的那種。

1

一般而言,如果您的視圖足夠簡單以便僅使用其中的一個或兩個,則可以使用LinearLayout。但是,如果您發現自己嵌套了很多LinearLayout,那麼您應該使用RelativeLayout切換到此標誌。使用單個RelativeLayout比許多LinearLayout更有效率。

如果您提供了佈局樣式的示例,我們可以提供更具體的建議。