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. 第11章:MongoDB-CRUD操作--文档--查询

    ①语法 db.collection.find(query, projection) ②参数 query :可选,使用查询操作符指定查询条件 projection :可选,使用投影操作符指定返回的键.查 ...

  2. WordPaster-KesionCMS V9整合教程

    注意:KesionCMS V9使用的是JQuery 1.10.3版本.需要到JQuery UI官网下载JQuery 1.10.3的UI库. JQueryUI官网:http://jqueryui.com ...

  3. 2.3.4volatile的原子性

    关键字volatile虽然增加了实例变量在多个线程之间的可见性,但它却不具备同步性,那么也不具备原子性. 测试 package com.cky.thread; /** * Created by edi ...

  4. zend studio 修改字体大小

    第一步:进入设置窗口    windows -> preferences第二步:进入修改字体的选项卡.    General -> Appearance -> Colors and ...

  5. .NET中的FileUpload控件的使用-Jquery(一)

    FileUpload在HTML中是个常用的基础控件,在涉及到上传各种格式的文件时候都会用到:笔者前段时间正好用到它做上传功能,记录下来做一些累积, 前端到后台用的是的Jquery中的Ajax进行数据传 ...

  6. .net core Area独立成单独的dll文件

    以前做MES项目遇到过这个情况,一个项目有7到8个大模块,生产.质量.物耗.电子看板.设备等,每个模块都有大量业务,这样使用mvc结构如果所有模块放在一个目录中,那么势必会产生很多问题,各模块代码不好 ...

  7. ABP框架入门踩坑-配置数据库表前缀

    配置数据库表前缀 ABP踩坑记录-目录 本篇其实和ABP关系并不大,主要是EF Core的一些应用-.-. 起因 支持数据库表前缀应该是很多应用中比较常见的功能,而在ABP中并没直接提供这一功能,所以 ...

  8. node.js实现WebSocket

    最近在学习“HTML5游戏开发实战”,其中第8章内容是使用WebSocket来构建多人游戏---<你画我猜>.然而在实现过程中,却一直出错: 客户端请求时,服务器端会报错并终止: 而浏览器 ...

  9. 微服务 - Eureka注册中心

    我们来解决微服务的第一问题,服务的管理. 服务中心对外提供服务,需要对外暴露自己的地址.而consumer(调用者)需要记录服务提供者的地址.将来地址出现变更,还需要及时更新.这在服务较少的时候并不觉 ...

  10. VMware中安装Contos

    1 检查BIOS虚拟化支持 2 新建虚拟机 3 新建虚拟机向导 4 创建虚拟空白光盘 5 安装Linux系统对应的CentOS版 6 虚拟机命名和定位磁盘位置 7 处理器配置,看自己是否是双核.多核 ...