當(dāng)前位置:
首頁(yè) >
FrameLayout(帧布局)的基本使用
發(fā)布時(shí)間:2025/4/5
34
豆豆
生活随笔
收集整理的這篇文章主要介紹了
FrameLayout(帧布局)的基本使用
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
FrameLayout又稱作幀布局,它相比于LinearLayout和RelativeLayout要簡(jiǎn)單很多,因?yàn)樗膽?yīng)用場(chǎng)景也少了很多。這種布局沒(méi)有方便的定位方式,所有的控件都會(huì)默認(rèn)擺放在布局的左上角。
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"><TextViewandroid:id="@+id/tv"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="This is TextView" /><ImageViewandroid:id="@+id/iv"android:layout_width="wrap_content"android:layout_height="wrap_content"android:src="@mipmap/ic_launcher" /></FrameLayout>效果圖:
可以看到,文字和圖片都是位于布局的左上角。由于ImageView是在TextView之后添加的,因此圖片壓在了文字的上面。
當(dāng)然除了這種默認(rèn)效果之外,我們還可以使用android:layout_gravity屬性來(lái)指定控件在布局中的對(duì)齊方式,這和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"><TextViewandroid:id="@+id/tv"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_gravity="left"android:text="This is TextView" /><ImageViewandroid:id="@+id/iv"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_gravity="right"android:src="@mipmap/ic_launcher" /></FrameLayout>效果圖:
總體來(lái)講,FrameLayout由于定位的欠缺,導(dǎo)致它的應(yīng)用場(chǎng)景也比較少,不過(guò)之后使用碎片的時(shí)候是可以使用到的。
?
?
?
?
?
《新程序員》:云原生和全面數(shù)字化實(shí)踐50位技術(shù)專家共同創(chuàng)作,文字、視頻、音頻交互閱讀總結(jié)
以上是生活随笔為你收集整理的FrameLayout(帧布局)的基本使用的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: AlertDialog(对话框)的基本使
- 下一篇: PercentFrameLayout(百