您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息

采用TabHost和RadioButton实现页面导航效果

2025/10/22 21:30:01发布27次查看
实现的效果不解释,主要是记录一下代码
先看一下xml布局:
<?xml version="1.0" encoding="utf-8"?><tabhost xmlns:android="schemas.android.com/apk/res/android" android:id="@android:id/tabhost" android:layout_width="fill_parent" android:layout_height="fill_parent" > <linearlayout android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <framelayout android:id="@android:id/tabcontent" android:layout_width="fill_parent" android:layout_height="0.0dip" android:layout_weight="1.0" /> <tabwidget android:id="@android:id/tabs" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_weight="0.0" android:background="@drawable/maintab_toolbar_bg" android:visibility="gone" /> <radiogroup android:id="@id/main_radio" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_gravity="bottom" android:background="@drawable/maintab_toolbar_bg" android:gravity="center_vertical" android:orientation="horizontal" > <radiobutton android:id="@id/radio_contact" style="@style/main_tab_bottom" android:drawabletop="@drawable/main_tab_contact_checked" android:text="@string/radio_contact" /> <radiobutton android:id="@id/radio_calllist" style="@style/main_tab_bottom" android:layout_margintop="0dip" android:layout_marginbottom="0dip" android:drawabletop="@drawable/main_tab_calllist_normal" android:text="@string/radio_calllist" /> <radiobutton android:id="@id/radio_sms" style="@style/main_tab_bottom" android:layout_margintop="0dip" android:layout_marginbottom="0dip" android:drawabletop="@drawable/main_tab_sms_normal" android:text="@string/radio_sms" /> <radiobutton android:id="@id/radio_setting" style="@style/main_tab_bottom" android:layout_margintop="0dip" android:layout_marginbottom="0dip" android:drawabletop="@drawable/nav_menu_me" android:text="@string/radio_setting" /> </radiogroup> </linearlayout></tabhost>
二、maintabactivity
package com.gs.app.main;import android.app.tabactivity;import android.content.intent; import android.os.bundle;import android.view.window;import android.widget.compoundbutton; import android.widget.compoundbutton.oncheckedchangelistener; import android.widget.radiobutton;import android.widget.tabhost; import com.gs.appblue.r;import com.gs.app.contact.contactslistactivity; import com.gs.app.contact.recentcallslistactivity; import com.gs.app.setting.setting; import com.gs.app.sms.actsmslist; public class maintabactivity extends tabactivity implementsoncheckedchangelistener {private tabhost mtabhost;private intent mcontactintent;private intent mcalllogintent;private intent msmsintent; private intent msetintent;public void oncreate(bundle savedinstancestate) {super.oncreate(savedinstancestate); requestwindowfeature(window.feature_no_title);setcontentview(r.layout.main_tab); this.mcontactintent = new intent(this, contactslistactivity.class); this.mcalllogintent = new intent(this, recentcallslistactivity.class); this.msmsintent = new intent(this, actsmslist.class); this.msetintent = new intent(this, setting.class); ((radiobutton) findviewbyid(r.id.radio_contact)).setoncheckedchangelistener(this); ((radiobutton) findviewbyid(r.id.radio_calllist)).setoncheckedchangelistener(this); ((radiobutton) findviewbyid(r.id.radio_sms)).setoncheckedchangelistener(this); ((radiobutton) findviewbyid(r.id.radio_setting)).setoncheckedchangelistener(this); setupintent();}private void setupintent() {this.mtabhost = gettabhost(); tabhost localtabhost = this.mtabhost; localtabhost.addtab(buildtabspec("a_tab", r.string.radio_contact,r.drawable.main_tab_contact_normal, this.mcontactintent));localtabhost.addtab(buildtabspec("b_tab", r.string.radio_calllist,r.drawable.main_tab_calllist_normal, this.mcalllogintent));localtabhost.addtab(buildtabspec("c_tab", r.string.radio_sms,r.drawable.main_tab_sms_normal, this.msmsintent));localtabhost.addtab(buildtabspec("d_tab", r.string.radio_setting,r.drawable.main_tab_setting_normal, this.msetintent)); localtabhost.setcurrenttab(0);}@overridepublic void oncheckedchanged(compoundbutton buttonview, boolean ischecked) {if (ischecked) {switch (buttonview.getid()) {case r.id.radio_contact:this.mtabhost.setcurrenttabbytag("a_tab"); break;case r.id.radio_calllist:this.mtabhost.setcurrenttabbytag("b_tab");break;case r.id.radio_sms:this.mtabhost.setcurrenttabbytag("c_tab");break;case r.id.radio_setting:this.mtabhost.setcurrenttabbytag("d_tab");break;}}}private tabhost.tabspec buildtabspec(string tag, int reslabel, int resicon,final intent content) {return this.mtabhost.newtabspec(tag).setindicator(getstring(reslabel),getresources().getdrawable(resicon)).setcontent(content);}}
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录 Product