当前位置: 移动技术网 > 移动技术>移动开发>Android > 移动应用开发——FrameLayout(帧布局)的基本使用

移动应用开发——FrameLayout(帧布局)的基本使用

2020年07月08日  | 移动技术网移动技术  | 我要评论
FrameLayout又称作帧布局,它相比于LinearLayout和RelativeLayout要简单很多,因为它的应用场景也少了很多。这种布局没有方便的定位方式,所有的控件都会默认摆放在布局的左上角activity_main代码:<?xml version="1.0" encoding="utf-8"?><FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layou

FrameLayout又称作帧布局,它相比于LinearLayout和RelativeLayout要简单很多,因为它的应用场景也少了很多。这种布局没有方便的定位方式,所有的控件都会默认摆放在布局的左上角
在这里插入图片描述
activity_main代码:

<?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">   
     <TextView    
           android:id="@+id/tv"  
           android:layout_width="wrap_content"
           android:layout_height="wrap_content"  
           android:text="This is TextView" />   
         <ImageView     
                android:id="@+id/iv" 
                android:layout_width="wrap_content"             
                android:layout_height="wrap_content"    
                android:src="@mipmap/ic_launcher" /> 
                   </FrameLayout>

效果图:
在这里插入图片描述
可以看到,文字和图片都是位于布局的左上角。由于ImageView是在TextView之后添加的,因此图片压在了文字的上面。
当然除了这种默认效果之外,我们还可以使用android:layout_gravity属性来指定控件在布局中的对齐方式,这和LinearLayout中的用法是相似的。
修改activity_main.xml中的代码:

<?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">
  <TextView     
        android:id="@+id/tv"     
        android:layout_width="wrap_content"  
        android:layout_height="wrap_content"    
        android:layout_gravity="left"      
        android:text="This is TextView" />
        <ImageView      
                  android:id="@+id/iv"
                  android:layout_width="wrap_content"
                  android:layout_height="wrap_content"     
                  android:layout_gravity="right"    
                  android:src="@mipmap/ic_launcher" /> 
                         </FrameLayout>

效果图:
在这里插入图片描述
在这里插入图片描述

本文地址:https://blog.csdn.net/wzyai/article/details/107168382

如您对本文有疑问或者有任何想说的,请点击进行留言回复,万千网友为您解惑!

相关文章:

验证码:
移动技术网