Android的Frangment的第一种声明方式

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>

原文地址:https://www.cnblogs.com/charlypage/p/10351921.html

时间: 2024-07-31 14:06:21

Android的Frangment的第一种声明方式的相关文章

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

这里我们以 List<Student> studs作为 source,但是注意,studs中的学生可以是分别属于不同的班级和年级 先看GroupBy的第一种声明: public static IEnumerable<IGrouping<TKey, TSource>> GroupBy<TSource, TKey>(this IEnumerable<TSource> source, Func<TSource, TKey> keySelec

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

1. 数组的三种声明方式 public class WhatEver { public static void main(String[] args) { //第一种 例: String[] test1 = new String[6]; test1[0] = "数组0"; test1[1] = "数组1"; //第二种 例: String[] test2 = {"数组0","数组1","数组2","

函数——函数的两种声明方式:函数声明&amp;函数表达式的区别(未完待续。。。)

一.函数声明中函数名是必须的:函数表达式中则是可选的 //函数声明 function sum(a, b) { return a + b; } alert(sum(1, 2)); //函数表达式 /* var s = function sum(a, b) { return a + b; } alert(s(1, 2)); */ var s = function(a, b) { return a + b; } alert(s(1, 2)); //以上两种都可以 二.用函数声明定义的函数,函数可以在函

Android中BroadcastReceiver的两种注册方式(静态和动态)详解

今天我们一起来探讨下安卓中BroadcastReceiver组件以及详细分析下它的两种注册方式. BroadcastReceiver也就是"广播接收者"的意思,顾名思义,它就是用来接收来自系统和应用中的广播.在Android系统中,广播体现在方方面面,例如当开机完成后系统会产生一条广播,接收到这条广播就能实现开机启动服务的功能:当网络状态改变时系统会产生一条广播,接收到这条广播就能及时地做出提示和保存数据等操作:当电池电量改变时,系统会产生一条广播,接收到这条广播就能在电量低时告知用户

Android 进阶6:两种序列化方式 Serializable 和 Parcelable

什么是序列化 我们总是说着或者听说着"序列化",它的定义是什么呢? 序列化 (Serialization)将对象的状态信息转换为可以存储或传输的形式的过程.在序列化期间,对象将其当前状态写入到临时或持久性存储区.以后,可以通过从存储区中读取或反序列化对象的状态,重新创建该对象. 二进制序列化保持类型保真度,这对于在应用程序的不同调用之间保留对象的状态很有用.例如,通过将对象序列化到剪贴板,可在不同的应用程序之间共享对象.您可以将对象序列化到流.磁盘.内存和网络等等.远程处理使用序列化&

android 截取验证码的两种实现方式

在进行手机验证码验证时,为了提升用户体验,实现自动截取验证填充的行式,实现这个功能有两种方法,分别是利用android的广播机制和android的ContentObserver 实现. 第一种的实现方法如下: /**  * 监听返回的验证码信息,并自动补充如验证码输入框中 [一级方法]  */ public BroadcastReceiver getMessageReceive = new BroadcastReceiver() {  String address; @Override  pub

Android之message的几种发送方式

//第一种方式 message message = new obtain(); message.what = 1; message.age1 = 1; message.age2 = 2; message.obj = "yuzhi"; message.sendMessage(message); //第二种方式 Message message = new obtain(handler); message.what = 1; message.age1 = 1; message.age2 =

【Android-View】浅谈Android换肤的几种实现方式

Android平台api没有特意为换肤提供一套简便的机制,这可能是外国的软件更注重功能和易用,不流行换肤.系统不提供直接支持,只能自行研究. 换肤,可以认为是动态替换资源(文字.颜色.字体大小.图片.布局文件……).这个使用编程语言来动态设置是可以做到的,例如使用View的 setBackgroundResource.setTextSize.setTextColor等函数.但我们不可能在每个activity里对页面里 的所有控件都通过调用这些函数来换肤,这样的程序代码难以维护.扩展,也违背了UI

0139 函数的两种声明方式

2.8.1 自定义函数方式(命名函数) 利用函数关键字 function 自定义函数方式 // 声明定义方式 function fn() {...} // 调用 fn(); 因为有名字,所以也被称为命名函数 调用函数的代码既可以放到声明函数的前面,也可以放在声明函数的后面 2.8.2 函数表达式方式(匿名函数) 利用函数表达式方式的写法如下: // 这是函数表达式写法,匿名函数后面跟分号结束 var fn = function(){...}: // 调用的方式,函数调用必须写到函数体下面 fn(