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

PHP中文网
发布: 2017-06-06 16:24:28
原创
2233人浏览过

实现的效果不解释,主要是记录一下代码

先看一下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

火龙果写作
火龙果写作

用火龙果,轻松写作,通过校对、改写、扩展等功能实现高质量内容生产。

火龙果写作 106
查看详情 火龙果写作
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);}}
登录后复制
最佳 Windows 性能的顶级免费优化软件
最佳 Windows 性能的顶级免费优化软件

每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。

下载
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
作者最新文章
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号