189 8069 5689

Android动态添加Fragment

郑州app开发Android动态添加Fragment。布局代码一共需要建立三个,但是三个布局都是比较简单,只是用textview来显示文字的。在这里就不做展示了。

成都创新互联公司-专业网站定制、快速模板网站建设、高性价比路北网站开发、企业建站全套包干低至880元,成熟完善的模板库,直接使用。一站式路北网站制作公司更省心,省钱,快速模板网站建设找我们,业务覆盖路北地区。费用合理售后完善,十载实体公司更值得信赖。

MainActivity.java

package cn.xhhkj.videoview;

import androidx.appcompat.app.AppCompatActivity;

import androidx.fragment.app.Fragment;

import androidx.fragment.app.FragmentManager;

import android.app.FragmentTransaction;

import android.media.Mediaplayer;

import android.os.bundle;

import android.util.Log;

import android.view.Menu;

import android.view.View;

import android.widget.MediaController;

import android.widget.VideoView;

public class MainActivity extends AppCompatActivity {

    private static final String TAG = "结果是";

    @Override

    protected void onCreate(bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

        int width = getWindowManager().getDefaultDisplay().getWidth();

        int height = getWindowManager().getDefaultDisplay().getHeight();

        Log.d(TAG,"宽度"+width+"高度"+height);

        //FragmentManager manager = getFragmentManager();

       FragmentManager manager = getSupportFragmentManager();

       androidx.fragment.app.FragmentTransaction transaction = manager.beginTransaction();

       // FragmentTransaction transaction = manager.beginTransaction();

        Secondfragment second=new Secondfragment();

        if(width>height){

            transaction.replace(R.id.fragment_container,second);

        }else {

            transaction.replace(R.id.fragment_container,new Firstfragment());

        }

        transaction.commit();

    }

}

Firstfragment.java

package cn.xhhkj.videoview;

import android.os.bundle;

import android.view.LayoutInflater;

import android.view.View;

import android.view.ViewGroup;

import androidx.annotation.NonNull;

import androidx.annotation.Nullable;

import androidx.fragment.app.Fragment;

public class Firstfragment extends Fragment {

    @Nullable

    @Override

    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable bundle savedInstanceState) {

        View view=inflater.inflate(R.layout.fist_fragment,null);

        return view;

    }

}

Secondfragment.java

package cn.xhhkj.videoview;

import android.os.bundle;

import android.view.LayoutInflater;

import android.view.View;

import android.view.ViewGroup;

import androidx.annotation.NonNull;

import androidx.annotation.Nullable;

import androidx.fragment.app.Fragment;

public class Secondfragment extends Fragment {

    @Nullable

    @Override

    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable bundle savedInstanceState) {

        View view=inflater.inflate(R.layout.second_fragment,null);

        return view;

    }

}


当前标题:Android动态添加Fragment
网页地址:http://jkwzsj.com/article/sccoph.html

其他资讯