zoukankan      html  css  js  c++  java
  • 创建使用fragment

    如何使用Fragment
    1:静态使用
    .在对应的Activity中添加Fragment组件
    <fragment
    android:id="@+id/fragment1"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:name="com.example.myfragment.LoginFragment"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.0"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintVertical_bias="0.0" />

    .自定义一个类继承于Fragment,实现其对应的生命周期的方法(onCreateView方法是必须的)
    class LoginFragment :Fragment(){
    override fun onCreateView(
    inflater: LayoutInflater,
    container: ViewGroup?,
    savedInstanceState: Bundle?
    ): View? {
    /*从View?可以得出,这里要的返回值是一个View,
    View就可以通过解析xml文件得到
    attachToRoot(最后一个参数)表示是否添加到父容器上,但是fragment
    会添加到父容器上去,如果填True的话,就会在已经添加了一层fragment
    的父容器上面在添加一层fragment,就会报错
    */
    return inflater.inflate(R.layout.fragment_login,container,false)
    }
    }

    .创建xml文件进行页面布局(res-layout文件下创建)
    <?xml version="1.0" encoding="utf-8"?>
    <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/purple_500">

    <TextView
    android:id="@+id/textView"
    android:layout_width="150dp"
    android:layout_height="50dp"
    android:layout_marginStart="50dp"
    android:layout_marginTop="50dp"
    android:background="#D13535"
    android:text="Login"
    android:textSize="24sp"
    android:textStyle="bold"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.44"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintVertical_bias="0.578" />

    <Button
    android:id="@+id/button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Button"
    app:layout_constraintBottom_toTopOf="@+id/textView"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

    </androidx.constraintlayout.widget.ConstraintLayout>

    .使用LayoutInflter解析布局文件
      override fun onCreateView(
    inflater: LayoutInflater,
    container: ViewGroup?,
    savedInstanceState: Bundle?
    ): View? {
    /*从View?可以得出,这里要的返回值是一个View,
    View就可以通过解析xml文件得到
    attachToRoot(最后一个参数)表示是否添加到父容器上,但是fragment
    会添加到父容器上去,如果填True的话,就会在已经添加了一层fragment
    的父容器上面在添加一层fragment,就会报错
    */
    return inflater.inflate(R.layout.fragment_login,container,false)
    }

    .在xml中设置fragment对应的name属性(这一步是将fragment类与xml的fragment关联起来)
    但同时这也就表示该fragment是绑定死了一个fragment的,是没法变的
     android:name="com.example.myfragment.LoginFragment"

    2:动态使用
    .在对应的Activity中添加FramLayout
    .将静态创建方法中的fragment改成FrameLayout,用FrameLayout来占位:(
    <fragment
    .......>改成<FrameLayout
    .........>

    .自定义一个类继承于Fragment,实现其对应的生命周期的方法(onCreateView方法是必须的)
    .创建xml文件进行页面布局
    .使用LayoutInflter解析布局文件
    .使用FragmentManager来管理fragment的切换,(通过SupportedFragmentMananger获取
    FragmentManager通过获取supportFragmentManager.beginTransaction()来对fragment
    进行add(),replace(),remove()操作)
    val fragmentTransaction = supportFragmentManager.beginTransaction()
    fragmentTransaction.add(R.id.fragment1,LoginFragment())
    fragmentTransaction.commit()

    .fragmentTransaction.commit()通过commit来提交
    fragmentTransaction.commit()
  • 相关阅读:
    记php多张图片合成一张图片 压缩固定分辨率 合并生成竖列 纵向长图(可用于商品详情图合并下载)
    记php-mysql分页查询出现重复数据
    记laravel order by 问题
    记登录注册时候 前端js明文密码 加密传输 php解密
    记下载oss图片接口(附带删除)
    记tp5.1使用composer PhpOffice的xlsx表格文件导入数据库
    记php移动并压缩多级目录文件为zip文件并上传oss
    Jmeter服务器性能监控工具插件之ServerAgent
    Jmeter阶梯式加压测试
    Jmeter 下载+安装+汉化+版本更新+备份使用(Jmeter 4+版本均适用)
  • 原文地址:https://www.cnblogs.com/luofangli/p/14968917.html
Copyright © 2011-2022 走看看