FrameLayout就是屏幕上的一个“定位器”,可以使用它去显示一个单一的视图。被添加到FrameLayout上的视图views总是被固定在这个布局的左上角。考虑以下的代码:
- <?xml version="1.0" encoding="utf-8"?>
- <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:id="@+id/RLayout"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent" >
- <TextView
- android:id="@+id/lblComments"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_alignParentLeft="true"
- android:layout_alignParentTop="true"
- android:text="Hello, Android!" />
- <FrameLayout
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_alignLeft="@+id/lblComments"
- android:layout_below="@+id/lblComments"
- android:layout_centerHorizontal="true" >
- <ImageView
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:src="@drawable/droid" >
- </ImageView>
- </FrameLayout>
- </RelativeLayout>
但是,如果想要在这个FrameLayuout中添加另外的view(比如一个Button),那么这个view就会重叠在“之前的”view上面(本例中是显示图片的ImageView)。代码:
- <?xml version="1.0" encoding="utf-8"?>
- <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:id="@+id/RLayout"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent" >
- <TextView
- android:id="@+id/lblComments"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_alignParentLeft="true"
- android:layout_alignParentTop="true"
- android:text="Hello, Android!" />
- <FrameLayout
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_alignLeft="@+id/lblComments"
- android:layout_below="@+id/lblComments"
- android:layout_centerHorizontal="true" >
- <ImageView
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:src="@drawable/droid" >
- </ImageView>
- <Button
- android:layout_width="124dp"
- android:layout_height="wrap_content"
- android:text="Print Picture" />
- </FrameLayout>
- </RelativeLayout>
ScrollView是一种特殊的FrameLayout,使用ScrollView可以使用户能够滚动一个包含views的列表,这样做的话,就可以利用比物理显示区域更大的空间。有一点需要注意一下,那就是ScrollView只能包含一个子视图view或ViewGroup(这个ViewGroup通常是LinearLayout)。
ScrollView是一种特殊的FrameLayout,使用ScrollView可以使用户能够滚动一个包含views的列表,这样做的话,就可以利用比物理显示区域更大的空间。有一点需要注意一下,那就是ScrollView只能包含一个子视图view或ViewGroup(这个ViewGroup通常是LinearLayout)。
不要混合使用ListView和ScrollView。ListView被设计用来显示一些相关的信息,同时,ListView也已经被优化了去显示大量的列表lists。
下面的main.xml显示了一个包含LinearLayout的ScrollView,在LinearLayuout中又包含了一些Button和EditText视图:
- <?xml version="1.0" encoding="utf-8"?>
- <ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent" >
- <LinearLayout
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:orientation="vertical" >
- <Button
- android:id="@+id/button1"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="Button1" />
- <Button
- android:id="@+id/button2"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="Button2" />
- <Button
- android:id="@+id/button3"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="Button3" />
- <EditText
- android:id="@+id/txt"
- android:layout_width="fill_parent"
- android:layout_height="600dp" />
- <Button
- android:id="@+id/button4"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="Button4" />
- <Button
- android:id="@+id/button5"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="Button5" />
- </LinearLayout>
- </ScrollView>
因为EditText自动获得了焦点,它充满了整个activity(因为它的高度被设置成了600dp)。如果想阻止这个EditText获得焦点,那么只需在<LinearLayout>元素中添加以下两个属性:
- <LinearLayout
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:orientation="vertical"
- android:focusable="true"
- android:focusableInTouchMode="true"
- >
但是有的时候可能想要这个EditText自动获取焦点,但是又不想软键盘自动地显示出来。想要阻止软键盘的出现,可以在AndroidManifext.xml中的<activity>节点中,添加如下的属性:
- <activity
- android:name=".LayoutActivity"
- android:label="@string/app_name"
- <!-- 注意这行代码 -->
- android:windowSoftInputMode="stateHidden"
- >
- <intent-filter>
- <action android:name="android.intent.action.MAIN" />
- <category android:name="android.intent.category.LAUNCHER" />
- </intent-filter>
- </activity>