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()
  • 相关阅读:
    深度学习网络调试技巧
    深度学习网络调参技巧
    用深度学习解决大规模文本分类问题
    [译] 理解 LSTM 网络
    一文学会用 Tensorflow 搭建神经网络
    tensorflow实现基于LSTM的文本分类方法
    用keras实现lstm 利用Keras下的LSTM进行情感分析
    TensorFlow练习7: 基于RNN生成古诗词
    【深度学习】文本生成
    Leetcode--easy系列5
  • 原文地址:https://www.cnblogs.com/luofangli/p/14968917.html
Copyright © 2011-2022 走看看