欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Kotlin Fragment的具體使用詳解

 更新時間:2022年10月09日 10:45:13   作者:知奕奕  
Fragment是Android3.0后引入的一個新的API,他出現(xiàn)的初衷是為了適應大屏幕的平板電腦, 當然現(xiàn)在他仍然是平板APP UI設計的寵兒,而且我們普通手機開發(fā)也會加入這個Fragment, 我們可以把他看成一個小型的Activity,又稱Activity片段

概念

fragment 可以用作一個 activity 內部的小分塊;

當我們從手機轉換到 pad 上時,整體界面會發(fā)生變化(比如由單列視圖變?yōu)殡p列),此時就需要 fragment 的參與了!

基本示例

在本實例中,我們要制作一個雙列視圖,左右列均為 fragment 構成

設置左右列布局文件

新建布局文件 left_frag.xmlright_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 標簽,在 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內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!

您可能感興趣的文章:

相關文章

  • Android對話框AlertDialog與DatePickerDialog及TimePickerDialog使用詳解

    Android對話框AlertDialog與DatePickerDialog及TimePickerDialog使用詳解

    這篇文章主要介紹了Android對話框中的提醒對話框AlertDialog、日期對話框DatePickerDialog、時間對話框TimePickerDialog使用方法,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習吧
    2022-09-09
  • Android中設置組件半透明和透明的效果示例

    Android中設置組件半透明和透明的效果示例

    這篇文章主要給大家介紹了Android中設置組件半透明和透明效果的相關資料,文中給出了詳細的介紹和示例代碼,對大家具有一定的參考學習價值,需要的朋友們下面來一起看看吧。
    2017-05-05
  • android繪制曲線和折線圖的方法

    android繪制曲線和折線圖的方法

    這篇文章主要介紹了android繪制曲線和折線圖的方法,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-09-09
  • Android高效安全加載圖片的方法詳解

    Android高效安全加載圖片的方法詳解

    Android開發(fā)中消耗內存較多一般都是在圖像上面,下面這篇文章主要給大家介紹了關于Android如何高效安全加載圖片的相關資料,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面來一起學習學習吧
    2019-02-02
  • Android客戶端程序Gradle如何打包

    Android客戶端程序Gradle如何打包

    這篇文章主要介紹了Android客戶端程序Gradle如何打包 的相關資料,需要的朋友可以參考下
    2016-01-01
  • Android 6.0 掃描不到 Ble 設備需開啟位置權限的方法

    Android 6.0 掃描不到 Ble 設備需開啟位置權限的方法

    今天小編就為大家分享一篇Android 6.0 掃描不到 Ble 設備需開啟位置權限的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-07-07
  • Android 模擬器的使用詳細介紹

    Android 模擬器的使用詳細介紹

    本文主要介紹Android 模擬器,在開發(fā)Android應用過程中大家都要使用模擬器對應用的測試,這里對模擬器的資料做了詳細整理,有興趣的小伙伴可以參考下
    2016-08-08
  • Android中顏色選擇器和改變字體顏色的實例教程

    Android中顏色選擇器和改變字體顏色的實例教程

    這篇文章主要介紹了Android中顏色選擇器和改變字體顏色的實例教程,其中改變字體顏色用到了ColorPicker顏色選擇器,需要的朋友可以參考下
    2016-04-04
  • Android頁面中引導蒙層的使用方法詳解

    Android頁面中引導蒙層的使用方法詳解

    這篇文章主要為大家詳細介紹了Android頁面中的引導蒙層使用方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2019-03-03
  • OpenGL ES紋理詳解

    OpenGL ES紋理詳解

    這篇文章主要為大家詳細介紹了OpenGL ES紋理的相關資料,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2018-05-05

最新評論