Android仿微信頁(yè)面底部導(dǎo)航效果代碼實(shí)現(xiàn)
大家在參考本地代碼的時(shí)候要根據(jù)需要適當(dāng)?shù)男薷?,里面有冗余代碼小編沒(méi)有刪除。好了,廢話不多說(shuō)了,一切讓代碼說(shuō)話吧!
關(guān)鍵代碼如下所示:
.java里面的主要代碼
public class MainActivity extends BaseActivity implements TabChangeListener {
private Fragment[] fragments;
private FragZaiXianYuYue fragZaiXianYuYue;
private FragDaoLuJiuYuan fragDaoLuJiuYuan;
private FragJiFenShangCheng fragJiFenShangCheng;
private FragMe fragMe;
private ImageView img_right;
private ImageView[] imagebuttons;
private ImageView img_me_notity;
private TextView[] textviews;
private int index;
private int currentTabIndex;// 當(dāng)前fragment的index
private int keyBackClickCount = 0;
private int indexChange;
private boolean isChangeTab = false;
@Override
protected void onCreate(Bundle arg0) {
// TODO Auto-generated method stub
super.onCreate(arg0);
setContentView(R.layout.mainactivity);
initView();
initTabView();
FragBYRecord.setTabChangeListener(this);
FragWXRecord.setTabChangeListener(this);
registerMessageReceiver();
}
@Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
isForeground = true;
requestMaintenance();
if (isChangeTab) {
if (indexChange == 0) {
viewTitle.setZhongJianText(R.string.frag_zxyy);
tv_left.setVisibility(View.GONE);
} else if (indexChange == 0) {
viewTitle.setZhongJianText(R.string.frag_dljy);
tv_left.setVisibility(View.GONE);
}
changeTab(indexChange);
isChangeTab = false;
}
}
@Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
isForeground = false;
}
@Override
protected void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
if(null != mMessageReceiver)
unregisterReceiver(mMessageReceiver);
}
private void initView() {
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(new OnClickListener() {
@Override
public void onClick(View arg0) {
Intent intent = new Intent(MainActivity.this,
MineActivity.class);
MineActivity.mFragValue = MineActivity.FRAG_SYSTEMMESSAGE;
startActivity(intent);
}
});
tv_left.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
fragMe.addNewCar();
}
});
}
private void initTabView() {
fragZaiXianYuYue = new FragZaiXianYuYue();
fragDaoLuJiuYuan = new FragDaoLuJiuYuan();
fragJiFenShangCheng = new FragJiFenShangCheng();
fragMe = new FragMe();
fragments = new Fragment[] { fragZaiXianYuYue, fragDaoLuJiuYuan,
fragJiFenShangCheng, fragMe };
imagebuttons = new ImageView[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 = new TextView[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));
// 添加顯示第一個(gè)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();
}
public void onTabClicked(View view) {
switch (view.getId()) {
case R.id.re_zzyy:
index = 0;
if (fragZaiXianYuYue != null) {
}
viewTitle.setZhongJianText(R.string.frag_zxyy);
tv_left.setVisibility(View.GONE);
changeTab(index);
break;
case R.id.re_dljy:
index = 1;
viewTitle.setZhongJianText(R.string.frag_dljy);
tv_left.setVisibility(View.GONE);
changeTab(index);
break;
case R.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;
case R.id.re_me:
index = 3;
viewTitle.setZhongJianText(R.string.frag_me);
tv_left.setVisibility(View.VISIBLE);
changeTab(index);
break;
}
}
public void changeTab(int index) {
if (currentTabIndex != index) {
FragmentTransaction trx = 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);
// 把當(dāng)前tab設(shè)為選中狀態(tài)
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:
<include layout="@layout/layout_bottom"/>
layout_bottom.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns: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" >
<!-- 普通底部導(dǎo)航 -->
<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仿微信頁(yè)面底部導(dǎo)航效果代碼實(shí)現(xiàn),希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
相關(guān)文章
Android工具欄頂出轉(zhuǎn)場(chǎng)動(dòng)畫的實(shí)現(xiàn)方法實(shí)例
這篇文章主要給大家介紹了關(guān)于Android工具欄頂出轉(zhuǎn)場(chǎng)動(dòng)畫的實(shí)現(xiàn)方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)各位Android開發(fā)者們具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2018-09-09
Android調(diào)用系統(tǒng)拍照裁剪圖片模糊的解決方法
這篇文章主要為大家詳細(xì)介紹了Android調(diào)用系統(tǒng)拍照裁剪圖片模糊的解決方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-01-01
Android里面的Service種類以及啟動(dòng)方式
Android中的Service分為前臺(tái)服務(wù)和后臺(tái)服務(wù),前臺(tái)服務(wù)需要亮身份牌并顯示通知,后臺(tái)服務(wù)則有啟動(dòng)方式選擇,包括startService和bindService,選擇啟動(dòng)方式應(yīng)根據(jù)任務(wù)類型和場(chǎng)景進(jìn)行,需要的朋友可以參考下2025-02-02
基于Flutter實(shí)現(xiàn)按位置大小比例布局的控件
做視頻監(jiān)控項(xiàng)目時(shí)需要需要展示多分屏,比如2x2、3x3、414等等,所以本文為大家介紹了如何基于Flutter實(shí)現(xiàn)按位置大小比例布局的控件,需要的可以參考一下2023-08-08
Android?NDK入門初識(shí)(組件結(jié)構(gòu)開發(fā)流程)
這篇文章主要為大家介紹了Android?NDK入門之初識(shí)組件結(jié)構(gòu)開發(fā)流程詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-08-08
Android實(shí)現(xiàn)用文字生成圖片的示例代碼
本篇文章主要介紹了Android實(shí)現(xiàn)用文字生成圖片的示例代碼,這里整理了詳細(xì)的代碼,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2017-08-08
android自定義view實(shí)現(xiàn)鐘表效果
這篇文章主要為大家詳細(xì)介紹了android自定義view實(shí)現(xiàn)鐘表效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-12-12
Android 使用jQuery實(shí)現(xiàn)item點(diǎn)擊顯示或隱藏的特效的示例
本篇文章主要介紹了Android 使用jQuery實(shí)現(xiàn)item點(diǎn)擊顯示或隱藏的特效的示例,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-03-03
Android開發(fā)中實(shí)現(xiàn)應(yīng)用的前后臺(tái)切換效果
這篇文章主要介紹了Android開發(fā)中實(shí)現(xiàn)應(yīng)用的前后臺(tái)切換效果的方法,文章最后還附帶了監(jiān)聽程序是否進(jìn)入后臺(tái)的判斷方法,需要的朋友可以參考下2016-02-02
Android中activity處理返回結(jié)果的實(shí)現(xiàn)方式
這篇文章主要介紹了Android中activity處理返回結(jié)果的實(shí)現(xiàn)方式,為了實(shí)現(xiàn)這個(gè)功能,Android提供了一個(gè)機(jī)制,跳轉(zhuǎn)到其他activity時(shí),再返回,可以接受到其他activity返回的值,無(wú)需再start新的當(dāng)前activity。需要的朋友可以參考下2016-12-12

