網站首頁 編程語言 正文
概念
fragment 可以用作一個 activity 內部的小分塊;
當我們從手機轉換到 pad 上時,整體界面會發生變化(比如由單列視圖變為雙列),此時就需要 fragment 的參與了!
基本示例
在本實例中,我們要制作一個雙列視圖,左右列均為 fragment 構成
設置左右列布局文件
新建布局文件 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 都需要一個單獨的類來對其頁面進行渲染,以及部分事件處理;
創建類 LeftFrag.kt
使該類繼承 Fragment,并實現方法,渲染 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 標簽,在 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
相關推薦
- 2022-07-30 Yarn調度器與調度算法
- 2022-12-09 Python中的main函數與import用法_python
- 2022-04-25 C#關于Func和Action委托的介紹詳解_C#教程
- 2024-07-15 pgsql設置主鍵自增
- 2022-06-25 python數據寫入Excel文件中的實現步驟_python
- 2023-03-23 React?Render?Props共享代碼技術_React
- 2022-04-07 一篇文章帶你學習Python3的高階函數_python
- 2023-02-07 Android?Service完整實現流程分析_Android
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支