1.创建一个StaticFragment.java文件继承Fragment类和一个static_fragment.xml文件完成fragment的布局。在StaticFragment.java中重载onCreateVIEw(……)方法,通过调用inflater.inflate(……)方法并传入布局资源ID生成fragment的视图资源,并绑定static_fragment.xml中的相关组件然后实现其功能。实现代码如下:
static_fragment.xml
<?xml version="1.0" enCoding="utf-8"?><linearLayout xmlns:androID="http://schemas.androID.com/apk/res/androID" xmlns:tools="http://schemas.androID.com/tools" androID:layout_wIDth="match_parent" androID:layout_height="match_parent" tools:context=".StaticFragment" androID:orIEntation="vertical"> <button androID:ID="@+ID/btn_fm" androID:layout_wIDth="match_parent" androID:layout_height="wrap_content" androID:text="这是fragment静态注册" androID:textAllCaps="false"> </button> <EditText androID:ID="@+ID/et_fm" androID:layout_wIDth="match_parent" androID:layout_height="wrap_content" androID:hint="请输入你要改变的内容:"> </EditText></linearLayout>
StaticFragment.java:
package com.example.myapplication;import androID.os.Bundle;import androID.vIEw.LayoutInflater;import androID.vIEw.VIEw;import androID.vIEw.VIEwGroup;import androID.Widget.button;import androID.Widget.EditText;import androIDx.annotation.NonNull;import androIDx.annotation.Nullable;import androIDx.fragment.app.Fragment;public class StaticFragment extends Fragment { private button mBtnFm; private EditText mEtFm; @Nullable @OverrIDe public VIEw onCreateVIEw(@NonNull LayoutInflater inflater, @Nullable VIEwGroup container, @Nullable Bundle savedInstanceState) { //fragment的视图资源是直接通过调用inflater.inflate(……)方法并传入布局资源ID生成的。 VIEw v = inflater.inflate(R.layout.static_fragment, container,false); mEtFm = v.findVIEwByID(R.ID.et_fm); mBtnFm = v.findVIEwByID(R.ID.btn_fm); mBtnFm.setonClickListener(new VIEw.OnClickListener() { @OverrIDe public voID onClick(VIEw v) { mBtnFm.setText(mEtFm.getText().toString()); } }); return v; }}
2.在主布局activity_main.xml文件中绑定fragment布局文件。实现代码如下:
activity_main.xml
<?xml version="1.0" enCoding="utf-8"?><linearLayout xmlns:androID="http://schemas.androID.com/apk/res/androID" xmlns:tools="http://schemas.androID.com/tools" androID:layout_wIDth="match_parent" androID:layout_height="match_parent" tools:context=".MainActivity" androID:orIEntation="vertical"> <TextVIEw androID:layout_wIDth="match_parent" androID:layout_height="wrap_content" androID:text="这是主布局" androID:textcolor="@color/colorAccent" androID:textSize="30sp"> </TextVIEw> <TextVIEw androID:layout_wIDth="match_parent" androID:layout_height="wrap_content" androID:text="下面是fragment的布局" androID:textcolor="@color/colorPrimaryDark" androID:textSize="30sp"> </TextVIEw> <fragment androID:ID="@+ID/static_fm" androID:name="com.example.myapplication.StaticFragment" androID:layout_wIDth="match_parent" androID:layout_height="wrap_content"> </fragment></linearLayout>
注意:布局文件中加fragment节点,name属性必须填写完整的路径
MainActivity.java
package com.example.myapplication;import androIDx.appcompat.app.AppCompatActivity;import androID.os.Bundle;public class MainActivity extends AppCompatActivity { @OverrIDe protected voID onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentVIEw(R.layout.activity_main); }}
演示:
<iframe allowfullscreen="true" data-mediaembed="bilibili" frameborder="0" ID="e8bthtnI-1580640584853" src="https://player.bilibili.com/player.HTML?aID=86308313"></iframe>AndroID——fragment静态注册演示
二、fragment动态注册创建方法及步骤1.新建一个项目,创建2个Fragment继承类分别为MyFragment1.java和MyFragment2.java,然后创建2个布局文件分别为fragment1.xml和fragment2.xml.详细代码如下:
fragment1.xml:
<?xml version="1.0" enCoding="utf-8"?><linearLayout xmlns:androID="http://schemas.androID.com/apk/res/androID" xmlns:tools="http://schemas.androID.com/tools" androID:layout_wIDth="match_parent" androID:layout_height="match_parent" tools:context=".MyFragment1" androID:gravity="center" androID:background="@color/colorPrimaryDark"> <TextVIEw androID:layout_wIDth="wrap_content" androID:layout_height="match_parent" androID:gravity="center" androID:text="@string/hello_blank_fragment" androID:textSize="30sp" androID:textAllCaps="false" androID:textcolor="#F70505"> </TextVIEw></linearLayout>
MyFragment1.java:
package com.example.myapplication;import androID.content.Context;import androID.net.Uri;import androID.os.Bundle;import androIDx.fragment.app.Fragment;import androID.vIEw.LayoutInflater;import androID.vIEw.VIEw;import androID.vIEw.VIEwGroup;public class MyFragment1 extends Fragment { @OverrIDe public VIEw onCreateVIEw(LayoutInflater inflater, VIEwGroup container, Bundle savedInstanceState) { // Inflate the layout for this fragment return inflater.inflate(R.layout.fragment1, container, false); }}
fragment2.xml:
<?xml version="1.0" enCoding="utf-8"?><linearLayout xmlns:androID="http://schemas.androID.com/apk/res/androID" xmlns:tools="http://schemas.androID.com/tools" androID:layout_wIDth="match_parent" androID:layout_height="match_parent" tools:context=".MyFragment2" androID:gravity="center" androID:background="@color/colorAccent"> <TextVIEw androID:layout_wIDth="wrap_content" androID:layout_height="match_parent" androID:gravity="center" androID:text="@string/hello_blank_fragment" androID:textSize="30sp" androID:textAllCaps="false" androID:textcolor="#03FAE3"> </TextVIEw></linearLayout>
MyFragment2.java:
package com.example.myapplication;import androID.os.Bundle;import androIDx.fragment.app.Fragment;import androID.vIEw.LayoutInflater;import androID.vIEw.VIEw;import androID.vIEw.VIEwGroup;public class MyFragment2 extends Fragment { @OverrIDe public VIEw onCreateVIEw(LayoutInflater inflater, VIEwGroup container, Bundle savedInstanceState) { // Inflate the layout for this fragment return inflater.inflate(R.layout.fragment2, container, false); }}
上述代码与静态创建的区别不大。
2.以代码的形式将fragment添加到activity需要在activity里直接调用FragmentManager。
FragmentManager fm = getSupportFragmentManager();
然后通过代码块:
FragmentTransaction ts = fm.beginTransaction();Fragment mfg1 = new MyFragment1();ts.add(R.ID.fragment_container,mfg1);ts.commit();
提交一个fragment事务。其核心是ts.add(……)方法。
详细代码如下:
activity_main.xml:
<?xml version="1.0" enCoding="utf-8"?><relativeLayout xmlns:androID="http://schemas.androID.com/apk/res/androID" xmlns:tools="http://schemas.androID.com/tools" androID:layout_wIDth="match_parent" androID:layout_height="match_parent" tools:context=".MainActivity"> <linearLayout androID:ID="@+ID/linear" androID:layout_wIDth="match_parent" androID:layout_height="wrap_content" androID:gravity="center" androID:layout_alignParentBottom="true"> <button androID:ID="@+ID/btn_dy1" androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content" androID:text="fragment1" androID:textcolor="@color/colorAccent" androID:textSize="30sp"> </button> <button androID:ID="@+ID/btn_dy2" androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content" androID:text="fragment2" androID:textcolor="@color/colorPrimaryDark" androID:textSize="30sp"> </button> </linearLayout> <FrameLayout androID:ID="@+ID/fragment_container" androID:layout_wIDth="match_parent" androID:layout_height="wrap_content"> </FrameLayout></relativeLayout>
注意:fragment模块一般用FrameLayout布局承载
MainActivity.java
package com.example.myapplication;import androIDx.appcompat.app.AppCompatActivity;import androIDx.fragment.app.Fragment;import androIDx.fragment.app.FragmentManager;import androIDx.fragment.app.FragmentTransaction;import androID.os.Bundle;import androID.vIEw.VIEw;import androID.Widget.button;public class MainActivity extends AppCompatActivity implements VIEw.OnClickListener { private button mBtnDy1; private button mBtnDy2; FragmentManager fm; Fragment mfg1; Fragment mfg2; @OverrIDe protected voID onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentVIEw(R.layout.activity_main); fm = getSupportFragmentManager(); mBtnDy1 = findVIEwByID(R.ID.btn_dy1); mBtnDy2 = findVIEwByID(R.ID.btn_dy2); mBtnDy1.setonClickListener(this); mBtnDy2.setonClickListener(this); } @OverrIDe public voID onClick(VIEw v) { clearSelection();//清除按钮状态 FragmentTransaction ts = fm.beginTransaction(); hIDeFragments(ts); switch (v.getID()){ case R.ID.btn_dy1: mBtnDy1.setBackgroundcolor(0xff0000ff); if(mfg1 == null){ mfg1 = new MyFragment1(); ts.add(R.ID.fragment_container,mfg1); }else { ts.show(mfg1); } break; case R.ID.btn_dy2: mBtnDy2.setBackgroundcolor(0xff0000ff); if(mfg2 == null){ mfg2 = new MyFragment2(); ts.add(R.ID.fragment_container,mfg2); }else { ts.show(mfg2); } break; default: break; } ts.commit(); }// 将所有的Fragment都置为隐藏状态。 private voID hIDeFragments(FragmentTransaction transaction) { if (mfg1 != null) { transaction.hIDe(mfg1); } if (mfg2 != null) { transaction.hIDe(mfg2); } }// 清除掉所有的选中状态。 private voID clearSelection() { mBtnDy1.setBackgroundcolor(0xffffffff); mBtnDy2.setBackgroundcolor(0xffffffff); }}
演示:
<iframe allowfullscreen="true" data-mediaembed="bilibili" frameborder="0" ID="CXMJW6o7-1580692380091" src="https://player.bilibili.com/player.HTML?aID=86328998"></iframe>AndroID——fragment动态注册演示
点赞1收藏分享文章举报大鹏学编程发布了4 篇原创文章 · 获赞 2 · 访问量 106私信 关注 总结以上是内存溢出为你收集整理的Android——fragment简单使用全部内容,希望文章能够帮你解决Android——fragment简单使用所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)