国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > Android > 正文

Android底部菜單欄實現的實例代碼

2019-10-22 18:11:23
字體:
來源:轉載
供稿:網友

 Android 使用RadioGroup 實現底部導航菜單欄。

一、主界面布局的實現:

先來張效果圖:

android,底部菜單,底部,菜單,代碼

介紹一下總體界面包括的內容:底部五個導航按鈕,主界面包括一個FrameLayout用來放五個Fragment。點擊底部按鈕會對應跳轉到指定的界面。

實現布局: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="com.garvey.activitys.MainActivity"> <FrameLayout android:id="@+id/id_fragment_content" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_above="@+id/id_diverline"></FrameLayout> <View android:id="@+id/id_diverline" android:layout_above="@+id/id_bottom_tags" android:layout_width="match_parent" android:layout_height="0.1dp" android:background="#C2C5CE"/> <LinearLayout android:id="@+id/id_bottom_tags" android:layout_width="match_parent" android:layout_height="55dp" android:layout_alignParentBottom="true" android:background="@drawable/bt_tag_bg" android:orientation="horizontal"> <RadioGroup  android:id="@+id/id_navcontent"  android:layout_width="match_parent"  android:layout_height="match_parent"  android:layout_gravity="center"  android:background="@color/transparent"  android:gravity="center"  android:orientation="horizontal">  <RadioButton  android:id="@+id/id_nav_btshouye"  android:layout_width="0dp"  android:checked="true"  android:layout_height="wrap_content"  android:layout_gravity="center"  android:layout_weight="1"  android:background="@color/transparent"  android:button="@null"  android:clickable="true"  android:drawablePadding="2dp"  android:drawableTop="@drawable/x_nav_menu_sy"  android:gravity="center"  android:onClick="switchView"  android:text="首頁"  android:textColor="@drawable/x_nav_menu_color"  android:textSize="10sp" />  <RadioButton  android:id="@+id/id_nav_btgouwu"  android:layout_width="0dp"  android:layout_height="wrap_content"  android:layout_gravity="center"  android:layout_weight="1"  android:background="@color/transparent"  android:button="@null"  android:clickable="true"  android:drawablePadding="2dp"  android:drawableTop="@drawable/x_nav_menu_gw"  android:gravity="center"  android:onClick="switchView"  android:text="購物"  android:textColor="@drawable/x_nav_menu_color"  android:textSize="10sp" />  <RadioButton  android:id="@+id/id_nav_btfengshang"  android:layout_width="0dp"  android:layout_height="wrap_content"  android:layout_gravity="center"  android:layout_weight="1"  android:background="@color/transparent"  android:button="@null"  android:clickable="true"  android:drawablePadding="2dp"  android:drawableTop="@drawable/x_nav_menu_fs"  android:gravity="center"  android:onClick="switchView"  android:text="風尚"  android:textColor="@drawable/x_nav_menu_color"  android:textSize="10sp" />  <RadioButton  android:id="@+id/id_nav_btshequ"  android:layout_width="0dp"  android:layout_height="wrap_content"  android:layout_gravity="center"  android:layout_weight="1"  android:background="@color/transparent"  android:button="@null"  android:clickable="true"  android:drawablePadding="2dp"  android:drawableTop="@drawable/x_nav_menu_sq"  android:gravity="center"  android:onClick="switchView"  android:text="社區"  android:textColor="@drawable/x_nav_menu_color"  android:textSize="10sp" />  <RadioButton  android:id="@+id/id_nav_btwode"  android:layout_width="0dp"  android:layout_height="wrap_content"  android:layout_gravity="center"  android:layout_weight="1"  android:background="@color/transparent"  android:button="@null"  android:clickable="true"  android:drawablePadding="2dp"  android:drawableTop="@drawable/x_nav_menu_wd"  android:gravity="center"  android:onClick="switchView"  android:text="我的"  android:textColor="@drawable/x_nav_menu_color"  android:textSize="10sp" /> </RadioGroup> </LinearLayout></RelativeLayout>

編寫selector 用來設置點擊后的背景變化:

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文件的書寫為:

<?xml version="1.0" encoding="utf-8"?><selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:drawable="@mipmap/tabitem_0" android:state_checked="true"></item> <item android:drawable="@mipmap/tabitem0"></item></selector>

編寫文字點擊后顏色的變化drawable:x_nav_menu_color.xml

<?xml version="1.0" encoding="UTF-8"?><selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:color="#CF75E9" android:state_checked="true"></item> <item android:color="#989898"></item></selector>

編寫底部菜單欄背景的drawablebt_tag_bg.xml:

<?xml version="1.0" encoding="utf-8"?><shape xmlns:android="http://schemas.android.com/apk/res/android"> <solid android:color="#FFFFFF" /></shape>

按照上述方式的就完成了底部菜單欄的布局方式,同時在布局的時候我們為每個RadioButton設置了點擊監聽器,監聽方法是:switchView(View view)。

二、布局的代碼實現

創建五個fragment,分別對應每個按鈕的界面,Fragment的代碼非常簡單,例如下面一個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需要判斷是否已經被加過parent, // 如果有parent需要從parent刪除,要不然會發生這個rootview已經有parent的錯誤。 ViewGroup parent = (ViewGroup) rootView.getParent(); if (parent != null) {  parent.removeView(rootView); } return rootView; } @Override public void onResume() { super.onResume(); }}

然后編寫MainActivity的代碼,首先確立界面對應的索引:

 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;

然后書寫對應的switchView(View view )方法來實現對應的界面切換:

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 { /** * 底部導航欄的widdget */ private RadioGroup mNavGroup; private FragmentTransaction mTransaction; /** * 五個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; } }}

源碼下載

以上就是這篇文章的全部內容了,希望本文的內容對大家的學習或者工作具有一定的參考學習價值,如果有疑問大家可以留言交流,謝謝大家對VEVB武林網的支持。


注:相關教程知識閱讀請移步到Android開發頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 景德镇市| 眉山市| 福清市| 舟曲县| 土默特左旗| 景德镇市| 绥阳县| 万年县| 宣汉县| 开平市| 沙雅县| 车致| 巴塘县| 林周县| 林口县| 嵩明县| 凤山县| 海城市| 闽侯县| 原阳县| 济南市| 甘孜县| 长阳| 宽甸| 任丘市| 得荣县| 吉首市| 施秉县| 新丰县| 积石山| 顺昌县| 安新县| 邮箱| 杭锦后旗| 巴塘县| 武陟县| 察隅县| 镇安县| 秭归县| 威海市| 昌宁县|