经验总结:
1.看到FrameLayout这个词,觉得Frame这个词觉得特别奇怪,觉得不解。去android sdk 的开发帮助文档看看,内容如下:
FrameLayout is designed to block out an area on the screen to display a single item. Generally, FrameLayout should be used to hold a single child view, because it can be difficult to organize child views in a way that's scalable to different screen sizes without the children overlapping each other. You can, however, add multiple children to a FrameLayout and control their position within the FrameLayout by assigning gravity to each child, using the android:layout_gravity
attribute.
Child views are drawn in a stack, with the most recently added child on top. The size of the FrameLayout is the size of its largest child (plus padding), visible or not (if the FrameLayout's parent permits). Views that are GONE
are used for sizing only if setConsiderGoneChildrenWhenMeasuring()
is set to true.
FrameLayout是用来遮住屏幕的一个区域来显示单个项(item)。FrameLayout应该用来放置一个单独的子视图,因为它很难在相互不遮挡的情况下,将对于不同屏幕大小可伸缩的子视图放置在一起。但是,你也可以通过对每个子视图gravity设置属性来控制它们的位置来实现刚刚说到的这种情况,也就是一个framelayout存在多个不相互重叠的子视图。
子视图在stack中绘制,最新的放在最上面。Framelayout大小取决于最大子视图的大小。
2.案例
下面举个简单例子来说明framelayout的用法:背景图片上添加登陆界面
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<ImageView
android:src="@drawable/ic_launcher"
android:scaleType="fitCenter"
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:layout_gravity="center"
/>
<TableLayout
android:id="@+id/login"
android:layout_height="250px"
android:layout_width="250px"
android:layout_gravity="bottom|center">
<TableRow android:id="@+id/firstrow"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_margin="2dp">
<TextView android:text="Name: ">
</TextView>
<EditText
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:background="#ffffff"
android:text="input name"
android:gravity="right"></EditText>
</TableRow>
<TableRow android:id="@+id/secondrow"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_margin="2dp">
<TextView android:text="Password: ">
</TextView>
<EditText
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:background="#ffffff"
android:text="input pwd"
android:gravity="right"></EditText>
</TableRow>
<TableRow android:id="@+id/secondrow"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_margin="2dp">
<Button android:text="Login" />
<Button android:text="cancel" />
</TableRow>
</TableLayout>
</FrameLayout>