網(wǎng)站首頁(yè) 編程語(yǔ)言 正文
1.Fragment的介紹
Android在3.0版本引入了Fragment功能,它非常類(lèi)似于Activity,可以像Activity一樣包含布局。
它出現(xiàn)的初衷是為了適應(yīng)大屏幕的平板電腦,使用Fragment我們可以把屏幕劃分成幾塊,合理利用屏幕空間。
Fragment通常是嵌套在Activity中使用。
2.靜態(tài)加載
步驟:
(1)定義Fragment控件的布局文件。
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".LeftFragment"> <TextView android:layout_width="match_parent" android:layout_height="match_parent" android:text="這是左邊" /> </FrameLayout>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".RightFragment"> <TextView android:layout_width="match_parent" android:layout_height="match_parent" android:text="這是右邊" /> </FrameLayout>
(2)自定義Fragment類(lèi),繼承自Fragment類(lèi)或者子類(lèi),同時(shí)實(shí)現(xiàn)onCreateView()方法,在方法中,通過(guò)inflater.inflate加載布局文件,接著返回其View。
class LeftFragment : Fragment() {
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
return inflater.inflate(R.layout.fragment_left, container, false)
}
}
class RightFragment : Fragment() {
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_right, container, false)
}
}
(3)在需要加載Fragment控件的Activity對(duì)應(yīng)的布局文件中添加Fragment標(biāo)簽,并設(shè)置name屬性為自定義fragment。
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity"> <fragment android:id="@+id/leftFrag" android:name="com.hui.fragment.LeftFragment" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1"/> <fragment android:id="@+id/rightFrag" android:name="com.hui.fragment.RightFragment" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1"/> </LinearLayout>
(4)最后在Activity的onCreate()方法中調(diào)用setContentView()加載布局。
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
}
}
3.動(dòng)態(tài)加載
步驟:
(1)通過(guò)getSupportFragmentManager()方法獲得FragmentManager對(duì)象。
val fragmentManager=supportFragmentManager
(2)開(kāi)啟事務(wù),通過(guò)beginTransaction()方法獲得FragmentTransaction對(duì)象。
val transaction=fragmentManager.beginTransaction()
(3)調(diào)用add()方法或者repalce()方法加載Fragment。
transaction.replace(R.id.rightLayout,fragment)
//replace()方法需要傳入容器的id和待添加的Fragment實(shí)例
(4)最后調(diào)用commit()方法提交事務(wù)。
transaction.commit()
原文鏈接:https://blog.csdn.net/weixin_63357306/article/details/128062466
相關(guān)推薦
- 2023-07-09 windows找不到gpedit.msc,請(qǐng)確定文件名是否正確
- 2023-05-03 深入了解一下C語(yǔ)言中的柔性數(shù)組_C 語(yǔ)言
- 2022-10-04 SQL語(yǔ)句中的DDL類(lèi)型的數(shù)據(jù)庫(kù)定義語(yǔ)言操作_MsSql
- 2022-12-29 react如何修改循環(huán)數(shù)組對(duì)象的數(shù)據(jù)_React
- 2022-02-26 網(wǎng)頁(yè)小圖標(biāo)和文字混排時(shí)如何對(duì)齊基準(zhǔn)線
- 2024-03-13 TypeError: Cannot read property ‘get‘ of undefined
- 2021-11-12 C語(yǔ)言打印某一年中某月的日歷_C 語(yǔ)言
- 2023-03-21 Android?Hilt依賴(lài)注入的實(shí)現(xiàn)淺析_Android
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲(chǔ)小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過(guò)濾器
- Spring Security概述快速入門(mén)
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯(cuò)誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實(shí)現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡(jiǎn)單動(dòng)態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對(duì)象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊(duì)列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支