Android仿微信页面底部导航效果代码实现
大家在参考本地代码的时候要根据需要适当的修改,里面有冗余代码小编没有删除。好了,废话不多说了,一切让代码说话吧!
关键代码如下所示:
.java里面的主要代码
publicclassMainActivityextendsBaseActivityimplementsTabChangeListener{ privateFragment[]fragments; privateFragZaiXianYuYuefragZaiXianYuYue; privateFragDaoLuJiuYuanfragDaoLuJiuYuan; privateFragJiFenShangChengfragJiFenShangCheng; privateFragMefragMe; privateImageViewimg_right; privateImageView[]imagebuttons; privateImageViewimg_me_notity; privateTextView[]textviews; privateintindex; privateintcurrentTabIndex;//当前fragment的index privateintkeyBackClickCount=0; privateintindexChange; privatebooleanisChangeTab=false; @Override protectedvoidonCreate(Bundlearg0){ //TODOAuto-generatedmethodstub super.onCreate(arg0); setContentView(R.layout.mainactivity); initView(); initTabView(); FragBYRecord.setTabChangeListener(this); FragWXRecord.setTabChangeListener(this); registerMessageReceiver(); } @Override protectedvoidonResume(){ //TODOAuto-generatedmethodstub super.onResume(); isForeground=true; requestMaintenance(); if(isChangeTab){ if(indexChange==0){ viewTitle.setZhongJianText(R.string.frag_zxyy); tv_left.setVisibility(View.GONE); }elseif(indexChange==0){ viewTitle.setZhongJianText(R.string.frag_dljy); tv_left.setVisibility(View.GONE); } changeTab(indexChange); isChangeTab=false; } } @Override protectedvoidonPause(){ //TODOAuto-generatedmethodstub super.onPause(); isForeground=false; } @Override protectedvoidonDestroy(){ //TODOAuto-generatedmethodstub super.onDestroy(); if(null!=mMessageReceiver) unregisterReceiver(mMessageReceiver); } privatevoidinitView(){ viewTitle=(ViewTitle)findViewById(R.id.title_bar); img_right=viewTitle.getYoubianView(); img_right.setVisibility(View.VISIBLE); tv_left=viewTitle.getZuobianTextView(); viewTitle.getZuobianView().setVisibility(View.GONE); tv_left.setText("XXXX"); img_right.setOnClickListener(newOnClickListener(){ @Override publicvoidonClick(Viewarg0){ Intentintent=newIntent(MainActivity.this, MineActivity.class); MineActivity.mFragValue=MineActivity.FRAG_SYSTEMMESSAGE; startActivity(intent); } }); tv_left.setOnClickListener(newOnClickListener(){ @Override publicvoidonClick(Viewarg0){ fragMe.addNewCar(); } }); } privatevoidinitTabView(){ fragZaiXianYuYue=newFragZaiXianYuYue(); fragDaoLuJiuYuan=newFragDaoLuJiuYuan(); fragJiFenShangCheng=newFragJiFenShangCheng(); fragMe=newFragMe(); fragments=newFragment[]{fragZaiXianYuYue,fragDaoLuJiuYuan, fragJiFenShangCheng,fragMe}; imagebuttons=newImageView[4]; imagebuttons[0]=(ImageView)findViewById(R.id.ib_zzyy); imagebuttons[1]=(ImageView)findViewById(R.id.ib_dljy); imagebuttons[2]=(ImageView)findViewById(R.id.ib_jfsc); imagebuttons[3]=(ImageView)findViewById(R.id.ib_me); img_me_notity=(ImageView)findViewById(R.id.ib_me_notity); imagebuttons[0].setSelected(true); viewTitle.setZhongJianText(R.string.frag_zxyy); textviews=newTextView[4]; textviews[0]=(TextView)findViewById(R.id.tv_ib_zzyy); textviews[1]=(TextView)findViewById(R.id.tv_dljy); textviews[2]=(TextView)findViewById(R.id.tv_jfsc); textviews[3]=(TextView)findViewById(R.id.tv_me); textviews[0] .setTextColor(getResources().getColor(R.color.color_yellow)); //添加显示第一个fragment getSupportFragmentManager().beginTransaction() .add(R.id.fragment_container,fragZaiXianYuYue) .add(R.id.fragment_container,fragDaoLuJiuYuan) .add(R.id.fragment_container,fragJiFenShangCheng) .add(R.id.fragment_container,fragMe).hide(fragDaoLuJiuYuan) .hide(fragJiFenShangCheng).hide(fragMe).show(fragZaiXianYuYue) .commit(); } publicvoidonTabClicked(Viewview){ switch(view.getId()){ caseR.id.re_zzyy: index=0; if(fragZaiXianYuYue!=null){ } viewTitle.setZhongJianText(R.string.frag_zxyy); tv_left.setVisibility(View.GONE); changeTab(index); break; caseR.id.re_dljy: index=1; viewTitle.setZhongJianText(R.string.frag_dljy); tv_left.setVisibility(View.GONE); changeTab(index); break; caseR.id.re_jfsc: if(SharePreferUtil.getBoolean(CommonString.HAVESERVICE,false)){ index=2; viewTitle.setZhongJianText(R.string.frag_jfsc); tv_left.setVisibility(View.GONE); fragJiFenShangCheng.requestJudgeCar(); changeTab(2); if(SharePreferUtil.getBoolean("isFristGoShop",true)){ SharePreferUtil.putBoolean("isFristGoShop",false); fragJiFenShangCheng.mengcengDialog(); } }else{ showAddServiceDialog(); } break; caseR.id.re_me: index=3; viewTitle.setZhongJianText(R.string.frag_me); tv_left.setVisibility(View.VISIBLE); changeTab(index); break; } } publicvoidchangeTab(intindex){ if(currentTabIndex!=index){ FragmentTransactiontrx=getSupportFragmentManager() .beginTransaction(); trx.hide(fragments[currentTabIndex]); if(!fragments[index].isAdded()){ trx.add(R.id.fragment_container,fragments[index]); } trx.show(fragments[index]).commit(); } imagebuttons[currentTabIndex].setSelected(false); //把当前tab设为选中状态 imagebuttons[index].setSelected(true); textviews[currentTabIndex].setTextColor(getResources().getColor( R.color.white)); textviews[index].setTextColor(getResources().getColor( R.color.color_yellow)); currentTabIndex=index; } mainactivity.xml: <includelayout="@layout/layout_bottom"/> layout_bottom.xml: <?xmlversion="1.0"encoding="utf-8"?> <LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:background="@color/black" android:orientation="vertical"> <!--普通底部导航--> <LinearLayout android:id="@+id/main_bottom" android:layout_width="match_parent" android:layout_height="54dp" android:gravity="center_vertical" android:orientation="horizontal"> <RelativeLayout android:id="@+id/re_zzyy" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" android:onClick="onTabClicked" android:padding="3dp"> <RelativeLayout android:layout_width="60dp" android:layout_height="match_parent" android:layout_centerHorizontal="true" android:gravity="center"> <ImageView android:id="@+id/ib_zzyy" android:layout_width="30dp" android:layout_height="28dp" android:layout_centerHorizontal="true" android:focusable="false" android:scaleType="centerInside" android:src="@drawable/tab_zaixianyuyue"/> <TextView android:id="@+id/tv_ib_zzyy" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@id/ib_zzyy" android:layout_centerHorizontal="true" android:layout_marginTop="3dp" android:textColor="@color/white" android:text="@string/frag_zxyy" android:textSize="12sp"/> </RelativeLayout> </RelativeLayout> <RelativeLayout android:id="@+id/re_dljy" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" android:onClick="onTabClicked" android:padding="3dp"> <RelativeLayout android:layout_width="60dp" android:layout_height="match_parent" android:layout_centerHorizontal="true" android:gravity="center"> <ImageView android:id="@+id/ib_dljy" android:layout_width="30dp" android:layout_height="28dp" android:layout_centerHorizontal="true" android:focusable="false" android:scaleType="centerInside" android:src="@drawable/tab_daolujiuyuan"/> <TextView android:id="@+id/tv_dljy" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@id/ib_dljy" android:layout_centerHorizontal="true" android:layout_marginTop="3dp" android:textColor="@color/white" android:text="@string/frag_dljy" android:textSize="12sp"/> </RelativeLayout> </RelativeLayout> <RelativeLayout android:id="@+id/re_jfsc" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" android:onClick="onTabClicked" android:padding="3dp"> <RelativeLayout android:layout_width="60dp" android:layout_height="match_parent" android:layout_centerHorizontal="true" android:gravity="center"> <ImageView android:id="@+id/ib_jfsc" android:layout_width="30dp" android:layout_height="28dp" android:layout_centerHorizontal="true" android:focusable="false" android:scaleType="centerInside" android:src="@drawable/tab_jifenshangcheng"/> <TextView android:id="@+id/tv_jfsc" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@id/ib_jfsc" android:layout_centerHorizontal="true" android:layout_marginTop="3dp" android:textColor="@color/white" android:text="@string/frag_jfsc" android:textSize="12sp"/> </RelativeLayout> </RelativeLayout> <RelativeLayout android:id="@+id/re_me" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" android:onClick="onTabClicked" android:padding="3dp"> <RelativeLayout android:layout_width="60dp" android:layout_height="match_parent" android:layout_centerHorizontal="true" android:gravity="center"> <ImageView android:id="@+id/ib_me" android:layout_width="30dp" android:layout_height="28dp" android:layout_centerHorizontal="true" android:focusable="false" android:scaleType="centerInside" android:src="@drawable/tab_me"/> <ImageView android:id="@+id/ib_me_notity" android:layout_width="5dp" android:layout_height="5dp" android:layout_alignTop="@+id/ib_me" android:layout_alignRight="@+id/ib_me" android:scaleType="centerInside" android:src="@drawable/app_34_icon_point_normal" android:visibility="gone"/> <TextView android:id="@+id/tv_me" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@id/ib_me" android:layout_centerHorizontal="true" android:layout_marginTop="3dp" android:textColor="@color/white" android:text="@string/frag_me" android:textSize="12sp"/> </RelativeLayout> </RelativeLayout> </LinearLayout> </LinearLayout>
以上所述是小编给大家介绍的Android仿微信页面底部导航效果代码实现,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对毛票票网站的支持!