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

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要简单很多,因为它的应用场景也少了很多。这种布局没有方便的定位方式,所有的控件都会默认摆放在布局的左上角
移动应用开发——FrameLayout(帧布局)的基本使用
activity_main代码:

<?xml version="1.0" encoding="utf-8"?> <FrameLayout xmlns:android="https://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> 

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

<?xml version="1.0" encoding="utf-8"?> <FrameLayout xmlns:android="https://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> 

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

本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。

ctvol管理联系方式QQ:251552304

本文章地址:https://www.ctvol.com/addevelopment/898078.html

(0)
上一篇 2021年10月22日
下一篇 2021年10月22日

精彩推荐