采用TabHost和RadioButton实现页面导航效果
2014-05-16 14:50
288 查看
实现的效果不解释,主要是记录一下代码
先看一下xml布局:
<?xml version="1.0" encoding="UTF-8"?>
<TabHost xmlns:android="http://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 implements
OnCheckedChangeListener {
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);
}
@Override
public 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);
}
}
先看一下xml布局:
<?xml version="1.0" encoding="UTF-8"?>
<TabHost xmlns:android="http://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 implements
OnCheckedChangeListener {
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);
}
@Override
public 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);
}
}
相关文章推荐
- Android进阶之采用TabHost和RadioButton实现底部页面导航
- 如何使用RadioGroup和RadioButton实现FragmentTabHost导航效果?
- TabHost结合RadioButton实现主页的导航效果
- 自定义RadioButton和ViewPager实现TabHost带滑动的页卡效果
- 实现单页面导航效果的jQuery插件 - SMINT
- 页面导航效果实现总结
- ReactNative基础(六)使用react-navigation实现页面导航布局效果(TabNavigator)
- 使用自定义RadioButton和ViewPager实现TabHost效果和带滑动的页卡效果。
- Android仿微信页面底部导航效果代码实现
- android之TabLayout实现PagerSlidingTabStrip,ViewpagerIndicator之类的效果滑动页面导航效果(类似网易新闻)
- jQuery+css实现百度百科的页面导航效果
- 实现单页面导航效果的jQuery插件 - SMINT
- Android 利用ViewPager实现底部圆点导航左右滑动效果以及Fragment页面切换
- 采用jquery easyui loading css效果实现加载时的页面覆盖功能
- 页面导航时,实现淡入淡出效果
- 实现单页面导航效果的jQuery插件 - SMINT
- Fragment嵌套ViewPager加上RadioButton实现微信页面滑动点击效果
- Android 中 TabHost与ViewPager结合实现首页导航效果
- RadioButton和ViewPager实现页面切换效果
- css 导航,菜单对应页面切换效果实现方法