2011-04-20 60 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更有效率。

如果您提供了布局样式的示例,我们可以提供更具体的建议。