Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> Android仿微信頁面底部導航效果代碼實現

Android仿微信頁面底部導航效果代碼實現

編輯:關於Android編程

大家在參考本地代碼的時候要根據需要適當的修改,裡面有冗余代碼小編沒有刪除。好了,廢話不多說了,一切讓代碼說話吧!

關鍵代碼如下所示:

.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;// 當前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));
// 添加顯示第一個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);
// 把當前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:
<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" >
<!-- 普通底部導航 -->
<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仿微信頁面底部導航效果代碼實現,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對本站網站的支持!

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved