欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

Android仿微信页面底部导航效果代码实现

程序员文章站 2024-03-06 22:22:32
大家在参考本地代码的时候要根据需要适当的修改,里面有冗余代码小编没有删除。好了,废话不多说了,一切让代码说话吧! 关键代码如下所示: .java里面的主要代码...

大家在参考本地代码的时候要根据需要适当的修改,里面有冗余代码小编没有删除。好了,废话不多说了,一切让代码说话吧!

关键代码如下所示:

.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仿微信页面底部导航效果代码实现,希望对大家有所帮助