<?xml version="1.0" enCoding="utf-8"?><relativeLayout xmlns:androID="http://schemas.androID.com/apk/res/androID" xmlns:app="http://schemas.androID.com/apk/res-auto" xmlns:tools="http://schemas.androID.com/tools" androID:layout_wIDth="match_parent" androID:orIEntation="vertical" androID:layout_height="match_parent" tools:context=".MainActivity"><!--在这个位置的话就是添加我我们的就是学习强国的下面的那个导航栏使用的是我们线性布局的方式--> <FrameLayout androID:ID="@+ID/fragment" app:layout_constraintleft_toleftOf="parent" app:layout_constraintRight_toRightOf="parent" app:layout_constrainttop_totopOf="parent" app:layout_constraintBottom_totopOf="@ID/home" androID:layout_wIDth="match_parent" androID:layout_height="wrap_content" /> <RadioGroup androID:ID="@+ID/home" androID:orIEntation="horizontal" androID:layout_alignParentBottom="true" androID:layout_wIDth="match_parent" androID:layout_height="wrap_content"> <Radiobutton androID:textcolor="@drawable/radiocolor" androID:ID="@+ID/qiangguotong" androID:text="强国通" androID:gravity="center" androID:button="@null" androID:layout_weight="1" androID:drawabletop="@drawable/qiangguotong" androID:layout_wIDth="0dp" androID:layout_height="wrap_content" /> <Radiobutton androID:textcolor="@drawable/radiocolor" androID:ID="@+ID/bailing" androID:text="百灵" androID:gravity="center" androID:button="@null" androID:layout_weight="1" androID:layout_wIDth="0dp" androID:drawabletop="@drawable/bailing" androID:layout_height="wrap_content" /> <Radiobutton androID:textcolor="@drawable/radiocolor" androID:ID="@+ID/xuexi" androID:text="学习" androID:checked="true" androID:gravity="center" androID:button="@null" androID:layout_weight="1" androID:layout_wIDth="0dp" androID:drawabletop="@drawable/xuexi" androID:layout_height="wrap_content" /> <Radiobutton androID:textcolor="@drawable/radiocolor" androID:ID="@+ID/tv" androID:text="电视台" androID:gravity="center" androID:button="@null" androID:layout_weight="1" androID:layout_wIDth="0dp" androID:drawabletop="@drawable/tv" androID:layout_height="wrap_content" /> <Radiobutton androID:textcolor="@drawable/radiocolor" androID:ID="@+ID/diantai" androID:text="电台" androID:gravity="center" androID:button="@null" androID:layout_weight="1" androID:layout_wIDth="0dp" androID:drawabletop="@drawable/daantai" androID:layout_height="wrap_content" /><!--现在的话我们仅仅是做出了我们的就动画的效果但是还没有做出我们的文字呢变幻的效果 现在的话就是要做出我们的文字变换的效果。--> </RadioGroup></relativeLayout>
mainActivity中java部分的代码:
package com.example.xuexiqiangguo;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.Widget.RadioGroup;public class MainActivity extends AppCompatActivity { RadioGroup radioGroup; FragmentManager fragmentManager; FragmentTransaction fragmentTransaction; Fragment qiangguotongFragment,bailing,xuexi,tv,dianshitai; @OverrIDe protected voID onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentVIEw(R.layout.activity_main); //添加我们radiogroup的监听事件 initVIEws(); //在我们的oncreate中将我们的这些碎片封装到一个方法中 initData(); hIDeAll(); //显示当前的视图 showCurrentVIEw(); } private voID hIDeAll() { } private voID showCurrentVIEw() { fragmentTransaction = fragmentManager.beginTransaction(); fragmentTransaction.show(xuexi); fragmentTransaction.commit(); } //通过我们的构造方法去初始化这5个碎片化对象 private voID initData() { qiangguotongFragment = new QiangguotongFragment(); bailing = new Bailing(); xuexi = new Xuexi(); tv = new Tv(); dianshitai = new Dianshitai(); fragmentManager = getSupportFragmentManager(); fragmentTransaction = fragmentManager.beginTransaction(); fragmentTransaction.add(R.ID.fragment,qiangguotongFragment); fragmentTransaction.add(R.ID.fragment,bailing); fragmentTransaction.add(R.ID.fragment,xuexi); fragmentTransaction.add(R.ID.fragment,tv); fragmentTransaction.add(R.ID.fragment,dianshitai); //提交我们的碎片: fragmentTransaction.commit(); } private voID initVIEws() { radioGroup = findVIEwByID(R.ID.home); radioGroup.setonCheckedchangelistener(new RadioGroup.OnCheckedchangelistener() { @OverrIDe public voID onCheckedChanged(RadioGroup radioGroup, int i) { hIDeAll(); fragmentTransaction = fragmentManager.beginTransaction(); switch(i){ case R.ID.qiangguotong: fragmentTransaction.show(qiangguotongFragment); break; case R.ID.bailing: fragmentTransaction.show(bailing); break; case R.ID.xuexi: fragmentTransaction.show(xuexi); break; case R.ID.tv: fragmentTransaction.show(tv); break; case R.ID.diantai: fragmentTransaction.show(dianshitai); break; } fragmentTransaction.commit(); } //在这个位置执行的 *** 作是图片功能的隐藏 *** 作 private voID hIDeAll() { fragmentTransaction = fragmentManager.beginTransaction(); fragmentTransaction.hIDe(qiangguotongFragment); fragmentTransaction.hIDe(bailing); fragmentTransaction.hIDe(xuexi); fragmentTransaction.hIDe(tv); fragmentTransaction.hIDe(dianshitai); fragmentTransaction.commit(); } }); }}
这部分的代码中都是关于我们的就是fragment的碎片:
package com.example.xuexiqiangguo;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 Bailing extends Fragment { //调用的是我们的oncreatvIEw方法 @Nullable @OverrIDe public VIEw onCreateVIEw(@NonNull LayoutInflater inflater, @Nullable VIEwGroup container, @Nullable Bundle savedInstanceState) { VIEw vIEw = inflater.inflate(R.layout.bailing,container,false); return vIEw; }}
总结 以上是内存溢出为你收集整理的Android的Fragment网页实战全部内容,希望文章能够帮你解决Android的Fragment网页实战所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)