Android 使用RadioGroup 實(shí)現(xiàn)底部導(dǎo)航菜單欄。
成都創(chuàng)新互聯(lián)公司專(zhuān)業(yè)為企業(yè)提供瀾滄網(wǎng)站建設(shè)、瀾滄做網(wǎng)站、瀾滄網(wǎng)站設(shè)計(jì)、瀾滄網(wǎng)站制作等企業(yè)網(wǎng)站建設(shè)、網(wǎng)頁(yè)設(shè)計(jì)與制作、瀾滄企業(yè)網(wǎng)站模板建站服務(wù),十年瀾滄做網(wǎng)站經(jīng)驗(yàn),不只是建網(wǎng)站,更提供有價(jià)值的思路和整體網(wǎng)絡(luò)服務(wù)。
一、主界面布局的實(shí)現(xiàn):
先來(lái)張效果圖:
介紹一下總體界面包括的內(nèi)容:底部五個(gè)導(dǎo)航按鈕,主界面包括一個(gè)FrameLayout用來(lái)放五個(gè)Fragment。點(diǎn)擊底部按鈕會(huì)對(duì)應(yīng)跳轉(zhuǎn)到指定的界面。
實(shí)現(xiàn)布局:activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
編寫(xiě)selector 用來(lái)設(shè)置點(diǎn)擊后的背景變化:
x_nav_menu_fs.xml,x_nav_menu_gw.xml,x_nav_menu_sq.xml,x_nav_menu_sy.xml,x_nav_menu_wd.xml
例如x_nav_menu_sy.xml文件的書(shū)寫(xiě)為:
<?xml version="1.0" encoding="utf-8"?>
編寫(xiě)文字點(diǎn)擊后顏色的變化drawable:x_nav_menu_color.xml
<?xml version="1.0" encoding="UTF-8"?>
編寫(xiě)底部菜單欄背景的drawablebt_tag_bg.xml:
<?xml version="1.0" encoding="utf-8"?>
按照上述方式的就完成了底部菜單欄的布局方式,同時(shí)在布局的時(shí)候我們?yōu)槊總€(gè)RadioButton設(shè)置了點(diǎn)擊監(jiān)聽(tīng)器,監(jiān)聽(tīng)方法是:switchView(View view)。
二、布局的代碼實(shí)現(xiàn)
創(chuàng)建五個(gè)fragment,分別對(duì)應(yīng)每個(gè)按鈕的界面,F(xiàn)ragment的代碼非常簡(jiǎn)單,例如下面一個(gè)Fragment:
package com.garvey.modules; import android.os.Bundle; import android.support.v4.app.Fragment; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import com.garvey.babyshop.R; /** * 作者: Garvey on 2016/6/13. * 郵箱: lianjiawei18@163.com */ public class ShouYeFragment extends Fragment{ // 緩存Fragment view private View rootView; private static ShouYeFragment shouYeFragment; public ShouYeFragment(){} public static ShouYeFragment getNewInstance(){ if (shouYeFragment ==null){ shouYeFragment =new ShouYeFragment(); } return shouYeFragment; } @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { if (rootView == null) { rootView = inflater.inflate(R.layout.fragment_shouye, container, false); } // 緩存的rootView需要判斷是否已經(jīng)被加過(guò)parent, // 如果有parent需要從parent刪除,要不然會(huì)發(fā)生這個(gè)rootview已經(jīng)有parent的錯(cuò)誤。 ViewGroup parent = (ViewGroup) rootView.getParent(); if (parent != null) { parent.removeView(rootView); } return rootView; } @Override public void onResume() { super.onResume(); } }
然后編寫(xiě)MainActivity的代碼,首先確立界面對(duì)應(yīng)的索引:
public static final int VIEW_SHOUYE_INDEX = 0; public static final int VIEW_GOUWU_INDEX = 1; public static final int VIEW_FENGSHANG_INDEX = 2; public static final int VIEW_SHEQU_INDEX = 3; public static final int VIEW_WODE_INDEX = 4; private int temp_position_index = -1;
然后書(shū)寫(xiě)對(duì)應(yīng)的switchView(View view )方法來(lái)實(shí)現(xiàn)對(duì)應(yīng)的界面切換:
public void switchView(View view) { switch (view.getId()) { case R.id.id_nav_btshouye: if (temp_position_index != VIEW_SHOUYE_INDEX) { //顯示 mTransaction = getSupportFragmentManager().beginTransaction(); mTransaction.replace(R.id.id_fragment_content, syFragemnt); mTransaction.commit(); } temp_position_index = VIEW_SHOUYE_INDEX; break; case R.id.id_nav_btgouwu: if (temp_position_index != VIEW_GOUWU_INDEX) { //顯示 mTransaction = getSupportFragmentManager().beginTransaction(); mTransaction.replace(R.id.id_fragment_content, gwFragment); mTransaction.commit(); } temp_position_index = VIEW_GOUWU_INDEX; break; case R.id.id_nav_btfengshang: if (temp_position_index != VIEW_FENGSHANG_INDEX) { //顯示 mTransaction = getSupportFragmentManager().beginTransaction(); mTransaction.replace(R.id.id_fragment_content, fsFragment); mTransaction.commit(); } temp_position_index = VIEW_FENGSHANG_INDEX; break; case R.id.id_nav_btshequ: if (temp_position_index != VIEW_SHEQU_INDEX) { //顯示 mTransaction = getSupportFragmentManager().beginTransaction(); mTransaction.replace(R.id.id_fragment_content, sqFragment); mTransaction.commit(); } temp_position_index = VIEW_SHEQU_INDEX; break; case R.id.id_nav_btwode: if (temp_position_index != VIEW_WODE_INDEX) { //顯示 mTransaction = getSupportFragmentManager().beginTransaction(); mTransaction.replace(R.id.id_fragment_content, wdFragment); mTransaction.commit(); } temp_position_index = VIEW_WODE_INDEX; break; } }
MainActivity的總代碼如下:
package com.garvey.activitys; import android.os.Bundle; import android.support.v4.app.Fragment; import android.support.v4.app.FragmentTransaction; import android.support.v7.app.AppCompatActivity; import android.view.View; import android.widget.RadioGroup; import com.garvey.babyshop.R; import com.garvey.modules.FengShangFragment; import com.garvey.modules.GouWuFragment; import com.garvey.modules.SheQuFragment; import com.garvey.modules.ShouYeFragment; import com.garvey.modules.WoDeFragment; public class MainActivity extends AppCompatActivity { /** * 底部導(dǎo)航欄的widdget */ private RadioGroup mNavGroup; private FragmentTransaction mTransaction; /** * 五個(gè)Fragments */ Fragment syFragemnt, gwFragment, fsFragment, sqFragment, wdFragment; public static final int VIEW_SHOUYE_INDEX = 0; public static final int VIEW_GOUWU_INDEX = 1; public static final int VIEW_FENGSHANG_INDEX = 2; public static final int VIEW_SHEQU_INDEX = 3; public static final int VIEW_WODE_INDEX = 4; private int temp_position_index = -1; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initView(); } private void initView() { mNavGroup = (RadioGroup) findViewById(R.id.id_navcontent); syFragemnt = ShouYeFragment.getNewInstance(); gwFragment = GouWuFragment.getNewInstance(); fsFragment = FengShangFragment.getNewInstance(); sqFragment = SheQuFragment.getNewInstance(); wdFragment = WoDeFragment.getNewInstance(); //顯示 mTransaction = getSupportFragmentManager().beginTransaction(); mTransaction.replace(R.id.id_fragment_content, syFragemnt); mTransaction.commit(); } public void switchView(View view) { switch (view.getId()) { case R.id.id_nav_btshouye: if (temp_position_index != VIEW_SHOUYE_INDEX) { //顯示 mTransaction = getSupportFragmentManager().beginTransaction(); mTransaction.replace(R.id.id_fragment_content, syFragemnt); mTransaction.commit(); } temp_position_index = VIEW_SHOUYE_INDEX; break; case R.id.id_nav_btgouwu: if (temp_position_index != VIEW_GOUWU_INDEX) { //顯示 mTransaction = getSupportFragmentManager().beginTransaction(); mTransaction.replace(R.id.id_fragment_content, gwFragment); mTransaction.commit(); } temp_position_index = VIEW_GOUWU_INDEX; break; case R.id.id_nav_btfengshang: if (temp_position_index != VIEW_FENGSHANG_INDEX) { //顯示 mTransaction = getSupportFragmentManager().beginTransaction(); mTransaction.replace(R.id.id_fragment_content, fsFragment); mTransaction.commit(); } temp_position_index = VIEW_FENGSHANG_INDEX; break; case R.id.id_nav_btshequ: if (temp_position_index != VIEW_SHEQU_INDEX) { //顯示 mTransaction = getSupportFragmentManager().beginTransaction(); mTransaction.replace(R.id.id_fragment_content, sqFragment); mTransaction.commit(); } temp_position_index = VIEW_SHEQU_INDEX; break; case R.id.id_nav_btwode: if (temp_position_index != VIEW_WODE_INDEX) { //顯示 mTransaction = getSupportFragmentManager().beginTransaction(); mTransaction.replace(R.id.id_fragment_content, wdFragment); mTransaction.commit(); } temp_position_index = VIEW_WODE_INDEX; break; } } }
源碼下載
以上就是這篇文章的全部?jī)?nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,如果有疑問(wèn)大家可以留言交流,謝謝大家對(duì)創(chuàng)新互聯(lián)的支持。