开发者

Android:Layout Guidance

开发者 https://www.devze.com 2023-02-27 08:54 出处:网络
I understand the functionality of liner and relative layout. But I am confused which one to pick for the activity since I can full fill my requirment with both of them.

I understand the functionality of liner and relative layout.

But I am confused which one to pick for the activity since I can full fill my requirment with both of them.

As I am working on emulator , So I think I am missing something, Is there any gui开发者_如何学Pythondance or litmus on when to use which layout ?


This is how I nest the various views. from this example you can see that I use the ScrollView at the lowest level so that the view can scroll easily.

I then use the linear layout ontop of the scrollview so that I can simply stack widgets on the screen row by row.

Finally, I use the RelativeLayout so that I can use the "layout_alignParentBottom" parameter and make the button show on the bottom of the view.

<?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>


Neither is inherently better than the other...use a RelativeLayout if you are going to do a lot of positioning with objects relative to each other...RelativeLayouts are more flexible but take a little more care to align correctly...a LinearLayout tends to be easier if you have items which can be placed neatly horizontally or vertically...If either work in your case just go with the one you're most comfortable with.


In general, you can use a LinearLayout if your view is simple enough to only use one or two of them. However, if you find yourself nesting a lot of LinearLayouts, that is a sign that you should switch to using a RelativeLayout. It is more efficient to use a single RelativeLayout than many LinearLayouts.

If you provide an example of what your Layouts look like, we can provide more specific advice.

0

精彩评论

暂无评论...
验证码 换一张
取 消