Kotlin Fragment的具體使用詳解
概念
fragment 可以用作一個(gè) activity 內(nèi)部的小分塊;
當(dāng)我們從手機(jī)轉(zhuǎn)換到 pad 上時(shí),整體界面會(huì)發(fā)生變化(比如由單列視圖變?yōu)殡p列),此時(shí)就需要 fragment 的參與了!
基本示例
在本實(shí)例中,我們要制作一個(gè)雙列視圖,左右列均為 fragment 構(gòu)成
設(shè)置左右列布局文件
新建布局文件 left_frag.xml 和 right_frag.xml
左列布局我們插入一個(gè)按鈕并居中;
<?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>右列布局我們則插入一個(gè)文本;
<?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 都需要一個(gè)單獨(dú)的類來(lái)對(duì)其頁(yè)面進(jìn)行渲染,以及部分事件處理;
創(chuàng)建類 LeftFrag.kt
使該類繼承 Fragment,并實(shí)現(xiàn)方法,渲染 fragment:
這里使用了 inflater 對(duì)頁(yè)面進(jìn)行注冊(cè);
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)
}
}主布局文件注冊(cè)
我們需要在主 activity 的布局文件中使用這兩個(gè) fragment;
直接添加兩個(gè) 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>這邊注冊(cè)完畢后就大功告成了,直接運(yùn)行看看成果把!
到此這篇關(guān)于Kotlin Fragment的具體使用詳解的文章就介紹到這了,更多相關(guān)Kotlin Fragment內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Android對(duì)話框AlertDialog與DatePickerDialog及TimePickerDialog使用詳解
這篇文章主要介紹了Android對(duì)話框中的提醒對(duì)話框AlertDialog、日期對(duì)話框DatePickerDialog、時(shí)間對(duì)話框TimePickerDialog使用方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)吧2022-09-09
Android 6.0 掃描不到 Ble 設(shè)備需開(kāi)啟位置權(quán)限的方法
今天小編就為大家分享一篇Android 6.0 掃描不到 Ble 設(shè)備需開(kāi)啟位置權(quán)限的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-07-07
Android中顏色選擇器和改變字體顏色的實(shí)例教程
這篇文章主要介紹了Android中顏色選擇器和改變字體顏色的實(shí)例教程,其中改變字體顏色用到了ColorPicker顏色選擇器,需要的朋友可以參考下2016-04-04
Android頁(yè)面中引導(dǎo)蒙層的使用方法詳解
這篇文章主要為大家詳細(xì)介紹了Android頁(yè)面中的引導(dǎo)蒙層使用方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-03-03

