Android动态添加Fragment

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>

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

时间: 2024-10-11 21:51:07

Android动态添加Fragment的相关文章

动态添加Fragment的报错信息

05-29 21:23:28.406: E/AndroidRuntime(23636): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.***.Main}: java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent

Fragment解析创建和传参,动态添加fragment

一下是个人的一些总结. 为fragment创建XML <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <

动态添加Fragment碎片

1.      创建待添加的碎片实例. 2.      获取到FragmentManager,在活动中可以直接调用getFragmentManager()方法得到. 3.      开启一个事务,通过调用beginTransaction()方法开启. 4.      向容器内加入碎片,一般使用replace()方法实现,需要传入容器的id和待添加的碎片实例 5.      提交事务,调用commit()方法来完成 package com.jia.fragmenttest; import andr

android 动态添加控件并实现每个子控件的点击事件

需求:我们要点击进入一家店铺,根据不同的店铺,显示不同条数的子条目 如:消毒间,洗菜间等...这些都是或多或少的,所以需要动态添加: 首先自定义View(linearLayout): package cn.qust.fang.widget; import io.vov.vitamio.MediaPlayer; import io.vov.vitamio.widget.MediaController; import io.vov.vitamio.widget.VideoView; import a

Android动态添加布局

//1.利用LayoutInflater的inflate动态加载XML mLinearLayout = (LinearLayout)findViewById(R.id.LinearLayout_ID); LayoutInflater layoutInflater = LayoutInflater.from(context); View view = layoutInflater.inflate(resource--需要加载的XML, null); XML:resource = R.layout.

Android 动态添加Spinner(.java文件内实现) 实现 改变spinner 内文字属性

动态添加spinner 控件 Spinner s = new Spinner(this); String []items={"自己定义的要显示的数组"}; my_SpinnerAdapter array_adapter =new my_SpinnerAdapter(this, android.R.layout.simple_spinner_item, items); array_adapter .setDropDownViewResource(android.R.layout.simp

android动态添加view

动态添加一个view. 首先,搞清楚是哪个view(子view),要添加到哪个view(父view)中:其次,子view的属性怎么设置:这两个问题明白了,就容易了. 举个例子:比如要在一个LinearLayout中添加一个Button,  子view是Button,父view是LinearLayout. 子view的属性就是通过LayoutParams来设置的,注意是LinearLayout.LayoutParams,因为子view的高度,宽度这些都是针对父view的,要告诉父view自己要占用

Android动态添加和移除布局

1 package com.hyang.administrator.studentproject; 2 3 import android.os.Bundle; 4 import android.support.v7.app.AppCompatActivity; 5 import android.view.LayoutInflater; 6 import android.view.View; 7 import android.widget.Button; 8 import android.widg

android 动态创建Fragment

前一遍文章我们讲了静态创建Fragment,这个在实际的开发中几乎不用,都是动态创建的,所谓动态创建就是根据某个条件动态创建Fragment, 现在创建一个android项目 dynamicFragment MainActivity.java package com.example.dynamicfragment; import android.app.Activity; import android.app.FragmentManager; import android.app.Fragmen