Fragment是Android3.0后引入的一个新的API,他出现的初衷是为了适应大屏幕的平板电脑,当然现在他仍然是平板APPUI设计的宠儿,而且我们普通手机开发也会加入这个Fragment,我们可以把他看成一个小型的Activity,又称Activity片段
概念
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>这边注册完毕后就大功告成了,直接运行看看成果把!
到此这篇关于Kotlin Fragment的具体使用详解的文章就介绍到这了,更多相关Kotlin Fragment内容请搜索编程学习网以前的文章希望大家以后多多支持编程学习网!
本文标题为:Kotlin Fragment的具体使用详解
基础教程推荐
- Android多返回栈技术 2023-04-15
- Android中的webview监听每次URL变化实例 2023-01-23
- Flutter手势密码的实现示例(附demo) 2023-04-11
- Android开发使用RecyclerView添加点击事件实例详解 2023-06-15
- IOS 播放系统提示音使用总结(AudioToolbox) 2023-03-01
- iOS开发教程之XLForm的基本使用方法 2023-05-01
- android studio按钮监听的5种方法实例详解 2023-01-12
- IOS应用内跳转系统设置相关界面的方法 2022-11-20
- 解决Android Studio突然不显示logcat日志的问题 2023-02-04
- Flutter绘图组件之CustomPaint使用详解 2023-05-12
