<p>像我这个有强迫症的人来说,自从TabActivity抛弃之后,再使用看到一个个警告和一条条划着的横线,心里很不舒服,现在终于下定决心用Fragment来替换掉TabActivity了!我的研究成果如下:</p><p><img src="/up_pic/201810/301050489725.jpg" title="301050489725.jpg" alt="1.jpg"/></p><p>首先是MainActivity,它需要继承FragmentActivity(这里是指:版本是3.0之前的继承FragmentActivity,3.0版本之后的继承Activity就可以),对于FragmentActivity的声明周期我就不过多介绍了,和Activity差不了多少,自己也能弄明白!下边是MainActivity的代码:</p><pre class="brush:java;toolbar:false">packagenet.loonggg.fragment; importandroid.os.Bundle; importandroid.support.v4.app.Fragment; importandroid.support.v4.app.FragmentActivity; importandroid.support.v4.app.FragmentManager; importandroid.support.v4.app.FragmentTransaction; importandroid.view.Window; importandroid.widget.RadioButton; importandroid.widget.RadioGroup; importandroid.widget.RadioGroup.OnCheckedChangeListener; publicclassMainActivityextendsFragmentActivity{ privateFragment[]mFragments; privateRadioGroupbottomRg; privateFragmentManagerfragmentManager; privateFragmentTransactionfragmentTransaction; privateRadioButtonrbOne,rbTwo,rbThree,rbFour; @Override protectedvoidonCreate(BundlesavedInstanceState){ super.onCreate(savedInstanceState); requestWindowFeature(Window.FEATURE_NO_TITLE); setContentView(R.layout.activity_main); mFragments=newFragment[3]; fragmentManager=getSupportFragmentManager(); mFragments[0]=fragmentManager.findFragmentById(R.id.fragement_main); mFragments[1]=fragmentManager.findFragmentById(R.id.fragement_search); mFragments[2]=fragmentManager .findFragmentById(R.id.fragement_setting); fragmentTransaction=fragmentManager.beginTransaction() .hide(mFragments[0]).hide(mFragments[1]).hide(mFragments[2]); fragmentTransaction.show(mFragments[0]).commit(); setFragmentIndicator(); } privatevoidsetFragmentIndicator(){ bottomRg=(RadioGroup)findViewById(R.id.bottomRg); rbOne=(RadioButton)findViewById(R.id.rbOne); rbTwo=(RadioButton)findViewById(R.id.rbTwo); rbThree=(RadioButton)findViewById(R.id.rbThree); bottomRg.setOnCheckedChangeListener(newOnCheckedChangeListener(){ @Override publicvoidonCheckedChanged(RadioGroupgroup,intcheckedId){ fragmentTransaction=fragmentManager.beginTransaction() .hide(mFragments[0]).hide(mFragments[1]) .hide(mFragments[2]); switch(checkedId){ caseR.id.rbOne: fragmentTransaction.show(mFragments[0]).commit(); break; caseR.id.rbTwo: fragmentTransaction.show(mFragments[1]).commit(); break; caseR.id.rbThree: fragmentTransaction.show(mFragments[2]).commit(); break; default: break; } } }); } }</pre><p>下边对应的是MainActivity的布局文件activity_main.xml:</p><pre class="brush:xml;toolbar:false">&lt;LinearLayoutxmlns:android=&quot;http://schemas.android.com/apk/res/android&quot; xmlns:tools=&quot;http://schemas.android.com/tools&quot; android:layout_width=&quot;match_parent&quot; android:layout_height=&quot;match_parent&quot; android:background=&quot;@drawable/activity_bg&quot; android:orientation=&quot;vertical&quot;&gt; &lt;!--上边主页面--&gt; &lt;fragment android:id=&quot;@+id/fragement_main&quot; android:name=&quot;net.loonggg.fragment.FragmentMain&quot; android:layout_width=&quot;fill_parent&quot; android:layout_height=&quot;fill_parent&quot; android:layout_weight=&quot;10&quot;/&gt; &lt;fragment android:id=&quot;@+id/fragement_search&quot; android:name=&quot;net.loonggg.fragment.FragmentSearch&quot; android:layout_width=&quot;fill_parent&quot; android:layout_height=&quot;fill_parent&quot; android:layout_weight=&quot;10&quot;/&gt; &lt;fragment android:id=&quot;@+id/fragement_setting&quot; android:name=&quot;net.loonggg.fragment.FragmentSetting&quot; android:layout_width=&quot;fill_parent&quot; android:layout_height=&quot;fill_parent&quot; android:layout_weight=&quot;10&quot;/&gt; &lt;!--底部菜单页面--&gt; &lt;RadioGroup android:id=&quot;@+id/bottomRg&quot; android:layout_width=&quot;fill_parent&quot; android:layout_height=&quot;wrap_content&quot; android:layout_weight=&quot;0.5&quot; android:background=&quot;@drawable/tab_footer_bg&quot; android:orientation=&quot;horizontal&quot;&gt; &lt;RadioButton android:id=&quot;@+id/rbOne&quot; style=&quot;@style/rg_btn_style&quot; android:checked=&quot;true&quot; android:drawableTop=&quot;@drawable/rb_one_btn_selector&quot; android:text=&quot;首页&quot;/&gt; &lt;RadioButton android:id=&quot;@+id/rbTwo&quot; style=&quot;@style/rg_btn_style&quot; android:drawableTop=&quot;@drawable/rb_two_btn_selector&quot; android:text=&quot;搜索&quot;/&gt; &lt;RadioButton android:id=&quot;@+id/rbThree&quot; style=&quot;@style/rg_btn_style&quot; android:drawableTop=&quot;@drawable/rb_three_btn_selector&quot; android:text=&quot;设置&quot;/&gt; &lt;/RadioGroup&gt; &lt;/LinearLayout&gt;</pre><p>这里为了大家方便,展示一下项目的布局图:</p><p><img src="/up_pic/201810/301050576794.jpg" title="301050576794.jpg" alt="2.jpg"/></p><p>再下边是要设计的首页界面,它是继承的Fragment,具体看代码:</p><pre class="brush:java;toolbar:false">packagenet.loonggg.fragment; importandroid.os.Bundle; importandroid.support.v4.app.Fragment; importandroid.view.LayoutInflater; importandroid.view.View; importandroid.view.ViewGroup; importandroid.widget.TextView; publicclassFragmentMainextendsFragment{ privateTextViewtv; @Override publicViewonCreateView(LayoutInflaterinflater,ViewGroupcontainer, BundlesavedInstanceState){ returninflater.inflate(R.layout.fragment_main,container,false); } @Override publicvoidonActivityCreated(BundlesavedInstanceState){ super.onActivityCreated(savedInstanceState); tv=(TextView)getView().findViewById(R.id.titleTv); tv.setText(&quot;首页&quot;); } }</pre><p>接着是对应的布局文件代码fragment_main.xml:</p><pre class="brush:xml;toolbar:false">&lt;?xmlversion=&quot;1.0&quot;encoding=&quot;utf-8&quot;?&gt; &lt;LinearLayoutxmlns:android=&quot;http://schemas.android.com/apk/res/android&quot; android:layout_width=&quot;match_parent&quot; android:layout_height=&quot;match_parent&quot; android:orientation=&quot;vertical&quot;&gt; &lt;include android:id=&quot;@+id/one_title&quot; layout=&quot;@layout/title_bar&quot;/&gt; &lt;TextView android:layout_width=&quot;fill_parent&quot; android:layout_height=&quot;fill_parent&quot; android:gravity=&quot;center&quot; android:text=&quot;这是首页&quot; android:textColor=&quot;#000000&quot;/&gt; &lt;/LinearLayout&gt;</pre><p>再接着是:搜索界面的代码:</p><pre class="brush:java;toolbar:false">packagenet.loonggg.fragment; importandroid.os.Bundle; importandroid.support.v4.app.Fragment; importandroid.view.LayoutInflater; importandroid.view.View; importandroid.view.ViewGroup; importandroid.widget.TextView; publicclassFragmentSearchextendsFragment{ privateTextViewtv; @Override publicViewonCreateView(LayoutInflaterinflater,ViewGroupcontainer, BundlesavedInstanceState){ returninflater.inflate(R.layout.fragment_search,container,false); } @Override publicvoidonActivityCreated(BundlesavedInstanceState){ super.onActivityCreated(savedInstanceState); tv=(TextView)getView().findViewById(R.id.titleTv); tv.setText(&quot;搜索&quot;); } @Override publicvoidonPause(){ super.onPause(); } }</pre><p>如上是对应的布局文件的代码fragment_search.xml:</p><pre class="brush:xml;toolbar:false">&lt;?xmlversion=&quot;1.0&quot;encoding=&quot;utf-8&quot;?&gt; &lt;LinearLayoutxmlns:android=&quot;http://schemas.android.com/apk/res/android&quot; android:layout_width=&quot;fill_parent&quot; android:layout_height=&quot;wrap_content&quot; android:orientation=&quot;vertical&quot;&gt; &lt;includelayout=&quot;@layout/title_bar&quot;/&gt; &lt;TextView android:layout_width=&quot;fill_parent&quot; android:layout_height=&quot;fill_parent&quot; android:gravity=&quot;center&quot; android:text=&quot;这是搜索界面&quot; android:textColor=&quot;#000000&quot;/&gt; &lt;/LinearLayout&gt;</pre><p>紧跟着是:设置界面的代码:</p><pre class="brush:java;toolbar:false">packagenet.loonggg.fragment; importandroid.os.Bundle; importandroid.support.v4.app.Fragment; importandroid.view.LayoutInflater; importandroid.view.View; importandroid.view.ViewGroup; importandroid.widget.TextView; publicclassFragmentSettingextendsFragment{ privateTextViewtv; @Override publicViewonCreateView(LayoutInflaterinflater,ViewGroupcontainer, BundlesavedInstanceState){ returninflater.inflate(R.layout.fragment_setting,container,false); } @Override publicvoidonActivityCreated(BundlesavedInstanceState){ super.onActivityCreated(savedInstanceState); tv=(TextView)getView().findViewById(R.id.titleTv); tv.setText(&quot;设置&quot;); } }</pre><p>当然一样,下边对应的是设置界面的布局文件代码fragment_setting.xml:</p><pre class="brush:xml;toolbar:false">&lt;?xmlversion=&quot;1.0&quot;encoding=&quot;utf-8&quot;?&gt; &lt;LinearLayoutxmlns:android=&quot;http://schemas.android.com/apk/res/android&quot; android:layout_width=&quot;fill_parent&quot; android:layout_height=&quot;wrap_content&quot; android:orientation=&quot;vertical&quot;&gt; &lt;includelayout=&quot;@layout/title_bar&quot;/&gt; &lt;TextView android:layout_width=&quot;fill_parent&quot; android:layout_height=&quot;fill_parent&quot; android:gravity=&quot;center&quot; android:text=&quot;这是设置页面&quot; android:textColor=&quot;#000000&quot;/&gt; &lt;/LinearLayout&gt;</pre><p>最后是我用的title_bar.xml文件,这个文件是嵌入到各个界面中的那个顶部的标题的布局文件,代码如下:</p><pre class="brush:xml;toolbar:false">&lt;?xmlversion=&quot;1.0&quot;encoding=&quot;utf-8&quot;?&gt; &lt;LinearLayoutxmlns:android=&quot;http://schemas.android.com/apk/res/android&quot; android:layout_width=&quot;fill_parent&quot; android:layout_height=&quot;wrap_content&quot; android:background=&quot;@drawable/title_bg&quot; android:gravity=&quot;center&quot; android:orientation=&quot;vertical&quot;&gt; &lt;TextView android:id=&quot;@+id/titleTv&quot; android:layout_width=&quot;wrap_content&quot; android:layout_height=&quot;wrap_content&quot; android:layout_gravity=&quot;center&quot; android:gravity=&quot;center&quot; android:textColor=&quot;#ffffff&quot; android:textSize=&quot;20sp&quot;/&gt; &lt;/LinearLayout&gt;</pre>
返回顶部 留言