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>


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM