網(wǎng)站首頁 編程語言 正文
概念
fragment 可以用作一個 activity 內(nèi)部的小分塊;
當(dāng)我們從手機轉(zhuǎn)換到 pad 上時,整體界面會發(fā)生變化(比如由單列視圖變?yōu)殡p列),此時就需要 fragment 的參與了!
基本示例
在本實例中,我們要制作一個雙列視圖,左右列均為 fragment 構(gòu)成
設(shè)置左右列布局文件
新建布局文件 left_frag.xml
和 right_frag.xml
左列布局我們插入一個按鈕并居中;
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/left_btn" android:text="左邊的按鈕" android:layout_gravity="center_horizontal"/> </LinearLayout>
右列布局我們則插入一個文本;
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/right_text" android:text="右邊的frag" android:layout_gravity="center_horizontal"/> </LinearLayout>
配置左右布局類
一般的,所有 fragment 都需要一個單獨的類來對其頁面進行渲染,以及部分事件處理;
創(chuàng)建類 LeftFrag.kt
使該類繼承 Fragment,并實現(xiàn)方法,渲染 fragment:
這里使用了 inflater 對頁面進行注冊;
package com.zhiyiyi.listviewdemo import android.os.Bundle import android.view.LayoutInflater import android.view.View import android.view.ViewGroup import androidx.fragment.app.Fragment class LeftFrag : Fragment() { override fun onCreateView( inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle? ): View? { return inflater.inflate(R.layout.left_frag, container, false) } }
主布局文件注冊
我們需要在主 activity 的布局文件中使用這兩個 fragment;
直接添加兩個 fragment 標(biāo)簽,在 name 屬性寫上 fragment 布局處理的類即可;
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <fragment android:id="@+id/leftfrag" android:name="com.zhiyiyi.listviewdemo.LeftFrag" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" /> <fragment android:id="@+id/rightfrag" android:name="com.zhiyiyi.listviewdemo.RightFrag" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" /> </LinearLayout>
這邊注冊完畢后就大功告成了,直接運行看看成果把!
原文鏈接:https://blog.csdn.net/delete_you/article/details/127186768
相關(guān)推薦
- 2022-03-14 跨域:Response to preflight request doesn t pass acce
- 2022-09-05 Shiro和Security的核心模塊以及兩者的區(qū)別
- 2022-10-09 Python?pygame項目實戰(zhàn)監(jiān)聽退出事件_python
- 2022-08-19 vscode遠程免密登入Linux服務(wù)器的配置方法_Linux
- 2022-11-24 詳解Linux中atime,mtime,ctime的使用場景_linux shell
- 2022-12-09 React文件分段上傳實現(xiàn)方法詳解_React
- 2022-11-23 Android10?Binder原理概述深入解析_Android
- 2024-03-03 layui 表格select下拉不顯示全的問題
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡單動態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支