博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Android动态添加Fragment
阅读量:4677 次
发布时间:2019-06-09

本文共 2500 字,大约阅读时间需要 8 分钟。

效果图如下:

1373932-20190205220338130-436743257.gif

项目结构图如下:

1373932-20190205220439429-87669283.png

Fragment1:

package com.demo.dongtaifragment;import android.app.Fragment;import android.os.Bundle;import android.support.annotation.NonNull;import android.support.annotation.Nullable;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;public class Fragment2 extends Fragment {    //显示faragemnt1 自己要显示的内容    @Nullable    @Override    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {        View view = inflater.inflate(R.layout.fragemnt2, null);        return view;    }}

Fragment2:

package com.demo.dongtaifragment;import android.app.Fragment;import android.os.Bundle;import android.support.annotation.NonNull;import android.support.annotation.Nullable;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;public class Fragment2 extends Fragment {    //显示faragemnt1 自己要显示的内容    @Nullable    @Override    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {        View view = inflater.inflate(R.layout.fragemnt2, null);        return view;    }}

MainActivity:

package com.demo.dongtaifragment;import android.app.FragmentManager;import android.app.FragmentTransaction;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.view.WindowManager;public class MainActivity extends AppCompatActivity {    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        //[1]获取手机的分辨率        WindowManager wm = (WindowManager) getSystemService(WINDOW_SERVICE);        int width = wm.getDefaultDisplay().getWidth();        int height = wm.getDefaultDisplay().getHeight();        FragmentManager fragmentManager  = getFragmentManager();        //开启事务        FragmentTransaction beginTransaction = fragmentManager.beginTransaction();        Fragment1 fragment1 = new Fragment1();        //判断横竖        if(height>width){            //说明是竖屏  加载一个fragment           beginTransaction.replace(android.R.id.content, new Fragment1());        }else{            //说明是横屏            beginTransaction.replace(android.R.id.content, new Fragment2());        }        //最后一步 记得commit        beginTransaction.commit();    }}

fragemnt1.xml:

fragemnt2.xml:

转载于:https://www.cnblogs.com/charlypage/p/10353231.html

你可能感兴趣的文章
彻底弄懂 RTL级,行为级的区别
查看>>
关于PHP开发的9条建议
查看>>
jackson的自动检测机制
查看>>
2019 计蒜之道 初赛 第二场 B. 百度AI小课堂-上升子序列(简单) ( 实现)
查看>>
Python(2.7)-随机函数(random)
查看>>
loadrunner测试c/s架构的应用系统
查看>>
Mybatis学习笔记(一) 之框架原理
查看>>
Aptana 安装与配置
查看>>
ABSTRACT的方法是否可同时是STATIC,是否可同时是NATIVE,是否可同时是SYNCHRONIZED?
查看>>
【SPL标准库专题(10)】SPL Exceptions
查看>>
《Python从入门基础到实践》
查看>>
【读入优化】
查看>>
python-网络编程urllib模块
查看>>
0029 Java学习笔记-面向对象-枚举类
查看>>
CGRectGet *** 获取控件坐标的方法
查看>>
SQL的主键和外键约束
查看>>
Bookmarklet
查看>>
c++primer 第l六章编程练习答案
查看>>
上海秋季HCC小记
查看>>
Illustrator 上色
查看>>