zoukankan      html  css  js  c++  java
  • 02Fragment的使用

    Fragment是什么

    Fragment碎片,可以理解为Activity的一个组成部分。

    Activity有生命周期,Fragment也有生命周期。(自己找资料看吧)

    Fragment代码

    在HelloWorld工程中(或者空工程)中实现。

    1. 在activity_main.xml文件中添加

      <fragment
              android:id="@+id/imageShowFragment"
              android:layout_width="300dp"
              android:layout_height="300dp"
              android:layout_gravity="center"
              android:layout_marginTop="5dp"
              />
      
    2. 在文件夹res/drawable中放一张图片tim1.png

    3. 在文件夹res/layout中,创建一个fragment_imageshow.xml文件。

      并添加ImageView控件

      <?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">
          
          <ImageView
              android:id="@+id/imageShowView"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:src="@drawable/time1"
              />
      
      </LinearLayout>
      
    4. 在文件夹java/xxx/中,创建一个ImageShowFragment.java文件。

    5. 在ImageShowFragment.java中,重写onCreateView方法,如下:

      表示:将fragment界面绑定到容器中。

      return inflater.inflate(R.layout.fragment_imageshow, container, false);
      
    6. 绑定到哪个容器了呢?在activity_main.xml中修改fragment如下:

    7. 打开虚拟手机,启动程序,则会显示图片。

    8. 在下一篇,将实现两个fragment之间的通信。

  • 相关阅读:
    u3d Mecanim动画
    四元数的共轭和逆
    c++ 单例模式
    GVIM、VIM
    Linux磁盘管理——虚拟文件系统
    Linux磁盘管理——MBR 与 GPT
    Link monitoring
    Linux高级网络设置——将多个网卡设置成一个网卡
    Linux高级网络设置——给网卡绑定多个IP
    DNS原理极限剖析
  • 原文地址:https://www.cnblogs.com/mingriyingying/p/14370357.html
Copyright © 2011-2022 走看看