首页  >  文章  >  web前端  >  采用TabHost和RadioButton实现页面导航效果

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

PHP中文网
PHP中文网原创
2017-06-06 16:24:281881浏览

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

先看一下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);}}
声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn