首页 > 移动平台 > 详细

Android动态添加Fragment

时间:2019-02-05 22:24:53      阅读:236      评论:0      收藏:0      [点我收藏+]

Android动态添加Fragment

效果图如下:

技术分享图片

项目结构图如下:

技术分享图片

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:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
    android:layout_height="match_parent">


    <TextView
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:text="我是fragment1竖屏的内容"
        android:textSize="20sp"
        />



</android.support.constraint.ConstraintLayout>

fragemnt2.xml:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
    android:layout_height="match_parent">


    <TextView
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:text="我是fragment2横屏的内容"
        android:textSize="30sp"

        />



</android.support.constraint.ConstraintLayout>

Android动态添加Fragment

原文:https://www.cnblogs.com/charlypage/p/10353231.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!