Android学习第三天之FrameLayout帧布局

来源:互联网 发布:淘宝网店手机可以开吗? 编辑:程序博客网 时间:2024/06/05 14:40

FrameLayout帧布局:

一  功能:

       (1)在这个布局中,所有的子元素都不能被指定放置的位置(不能指定某个空间或子布局的位置),他们统统放于这块区域的左上角,并且后面的子元素直接覆盖在前面的子元素之上,将前面的子元素部分和全部遮挡。

        (2)正因如此,所以FrameLayout布局没有像 android:gravity=""(指定该控件或布局 内容的位置)这样的属性。所以通常使用了此布局,并且想要改变此布局里面的子控件或子布局的位置时,只能在子控件或子布局中使用 android:layout_gravity=""属性。

        (3)在FreamLayout中,子控件是通过栈来绘制的,所以后添加的子控件会被控制在上层。

二 练习 :直接做以下练习


代码如下

<?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" >    <TextView        android:id="@+id/textView1"        android:layout_width="300dp"        android:layout_height="300dp"        android:gravity="right|bottom"        android:background="#456893"        android:textSize="60dp"        android:text="one" />    <TextView         android:id="@+id/textview2"        android:layout_width="200dp"        android:layout_height="200dp"        android:background="#879546"        android:gravity="right|bottom"        android:textSize="40dp"        android:text="two"/>    <TextView        android:id="@+id/textView3"        android:layout_width="130dp"        android:layout_height="130dp"        android:background="#635984"        android:gravity="bottom|right"        android:textSize="30dp"        android:text="three" />    <TextView        android:id="@+id/textView4"        android:layout_width="80dp"        android:layout_height="80dp"        android:gravity="center"        android:background="#987456"        android:textSize="20dp"        android:text="thrid"/></FrameLayout>
例如常见的一个进度条
<img src="http://img.blog.csdn.net/20151020184647505?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="" />

示例代码:

<?xml version="1.0" encoding="utf-8"?><FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent" >    <ProgressBar        android:id="@+id/progressBar1"        style="?android:attr/progressBarStyleLarge"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_gravity="center" />    <TextView         android:id="@+id/textview"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_gravity="center"        android:text="@string/textview"/></FrameLayout>
三:特有属性

android:foreground=""                              设置绘制在所有子控件之上的内容

 android:foregroundGravity=""                 设置绘制在所有子控件之上内容的gravity属性

例如下图:设置一个前景图,并且让它的位于右侧中间的位置


如图可知android:foreground=""  的意思就是把你所选的内容(我这里选了一张图片)覆盖在所有子控件之上

示例代码:

<?xml version="1.0" encoding="utf-8"?><FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:foreground="@drawable/ac"    android:foregroundGravity="right|center_vertical" >    <TextView        android:id="@+id/textView1"        android:layout_width="300dp"        android:layout_height="300dp"        android:gravity="right|bottom"        android:background="#456893"        android:layout_gravity="center"        android:textSize="60dp"        android:text="one" />    <TextView         android:id="@+id/textview2"        android:layout_width="200dp"        android:layout_height="200dp"        android:background="#879546"        android:gravity="right|bottom"        android:layout_gravity="center"        android:textSize="40dp"        android:text="two"/>    <TextView        android:id="@+id/textView3"        android:layout_width="130dp"        android:layout_height="130dp"        android:background="#635984"        android:gravity="bottom|right"        android:layout_gravity="center"        android:textSize="30dp"        android:text="three" />    <TextView        android:id="@+id/textView4"        android:layout_width="60dp"        android:layout_height="60dp"        android:gravity="center"        android:layout_gravity="center"        android:background="#987456"        android:textSize="20dp"        android:text="thrid"/></FrameLayout>


0 0
原创粉丝点击