Android的Frangment的第一种声明方式

实际效果图如下:

项目结构图如下:

fragment1:
package com.demo.fragementfirst;

import android.content.Context;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup; public class fragment1 extends Fragment { @Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { // 通过打气筒把一个布局转换成一个view对象
View view = inflater.inflate(
R.layout.fragment_fragment1, null
); return view; }
}
MainActivity:
package com.demo.fragementfirst;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.FrameLayout;
import android.widget.RelativeLayout; public class MainActivity extends AppCompatActivity { @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); }
}
MyFragment:
package com.demo.fragementfirst;

import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup; //定义fragment
public class MyFragment extends Fragment { // 当第一次画ui的时候调用,通过这个方法可以让fragment显示自己的布局内容
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // 通过打气筒把一个布局转换成一个view对象
View view = inflater.inflate(
R.layout.myfragment, null
); return view; }
}
activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
tools:context=".MainActivity"> <!--viewgroup可以有自己的孩子
通过 oncreateview 这个方法 fragment可以加载自己的布局 -->
<fragment
android:name="com.demo.fragementfirst.MyFragment"
android:id="@+id/list"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="match_parent"
>
</fragment> <fragment
android:name="com.demo.fragementfirst.fragment1"
android:id="@+id/viewer"
android:layout_weight="2"
android:layout_width="0dp"
android:layout_height="match_parent"
></fragment> </LinearLayout>
fragment_fragment1.xml:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".fragment1"> <!-- TODO: Update blank fragment layout -->
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="这是第二个fragemnt" /> </FrameLayout>
myfragment.xml:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".fragment1"> <!-- TODO: Update blank fragment layout -->
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#D64B28"
android:textColor="#FFFFFF"
android:text="这个是第一个fragment" /> </FrameLayout>

Android的Fragment的第一种声明方式的更多相关文章

  1. Android中Fragment的两种创建方式

    fragment是Activity中用户界面的一个行为或者是一部分.你可以在一个单独的Activity上把多个Fragment组合成为一个多区域的UI,并且可以在多个Activity中再使用.你可以认 ...

  2. GroupBy(..)的四种声明方式的理解及调用

    这里我们以 List<Student> studs作为 source,但是注意,studs中的学生可以是分别属于不同的班级和年级 先看GroupBy的第一种声明: public stati ...

  3. JavaScript 函数的两种声明方式

    1.函数声明的方式 JavaScript声明函数有两种选择:函数声明法,表达式定义法. 函数声明法 function sum (num1 ,num2){ return num1+num2 } 表达式定 ...

  4. 数组的三种声明方式总结、多维数组的遍历、Arrays类的常用方法总结

    1. 数组的三种声明方式 public class WhatEver { public static void main(String[] args) { //第一种 例: String[] test ...

  5. 第63天:json的两种声明方式

    一. json 两种声明方式 1. 对象声明   var  json = {width:100,height:100} 2. 数组声明   var  man = [        //  数组的 js ...

  6. Android传递Bitmap的两种简单方式及其缺陷

    Android传递Bitmap的几种简单方式 一,通过Intent的Bundle. 比如有两个activity,A,B,从A进入B.先在A中将Bitmap写进去: Resources res=getR ...

  7. Golang中map的三种声明方式和简单实现增删改查

    package main import ( "fmt" ) func main() { test3 := map[string]string{ "one": & ...

  8. 使用Typescript重构axios(二十)——请求取消功能:实现第一种使用方式

    0. 系列文章 1.使用Typescript重构axios(一)--写在最前面 2.使用Typescript重构axios(二)--项目起手,跑通流程 3.使用Typescript重构axios(三) ...

  9. js的数据类型、函数、流程控制及变量的四种声明方式

    运算符 基本运算符 加 + 减 - 乘 * 除 / 取余 % 自增 ++ eg: 1++ 或 ++1 自减 -- eg: 1-- 或 --1 注:++或--写在前面表示优先级最高,先进行自增或者自减 ...

随机推荐

  1. 第二章(java程序设计)第三章(语言基础)

    第二章 2.1 对象 对象的概念是由现实世界引入问题模型: 对象包含有:状态和行为.具体地来说是: 数据封装:对象的方法的作用就是:将内部变量封装起来,提供给外界交互的窗口.(实现对数据的隐藏) 继承 ...

  2. ZOJ 3156 Taxi (二分 + 二分匹配)

    题意:给定 n 个人坐标, m 辆车的坐标,还有人的速度,要求每个人要进一辆不同的车,问你所有都进车的最短时间是多少. 析:首先二分时间 mid,很明显就是最后那个人进车的时间,然后如果把第 i 个人 ...

  3. 如何在jsp和html页面上获取当前时间

    要想在JSP页面中获得当前时间并显示出来,首先得导入相关的Java包,然后创建Date对象. <%@page import="java.text.SimpleDateFormat,ja ...

  4. ACtiveMQ中间件-发布订阅模式

    前言:ActiveMQ学习心得 1.MQ是什么 MQ全称为Message Queue, 消息队列(MQ)是一种应用程序对应用程序的通信方法.应用程序通过读写出入队列的消息(针对应用程序的数据)来通信, ...

  5. TinyMCE Editor

    TinyMCE Editor(https://www.tinymce.com/features/) is an online text editor, it is used to write post ...

  6. hide handkerchief

    Problem Description The Children’s Day has passed for some days .Has you remembered something happen ...

  7. mysql_事务

    事务是针对数据的,不是针对结构的 存储引擎innodb支持事务,myisam不支持事务需求:有一张银行账户表,有A用户给B账户转账,A账户减少,B账户增加,但是A操作之后断电. 解决方案:A减少钱,但 ...

  8. whereis+whatis+man

    使用Linux过程中无论是使用shell命令.程序开发或者用户文档都需要使用到强大的男人man命令. 使用方法也十分简单,以查看ls命令的使用方法为例: man ls man的搜索路径通常包括以下两个 ...

  9. 1.buntu 安装redis

    方式一 :apt安装 在 Ubuntu 系统安装 Redi 可以使用以下命令: $sudo apt-get update $sudo apt-get install redis-server 启动 R ...

  10. cxRichEdit1获取EXCEL的区域图片

    cxRichEdit1获取EXCEL的区域图片   搞了好久却原来其实太简单: cxRichEdit1.Clear;Clipboard.Clear;ActiveSheet.cells[2, iCol] ...