使用FragmentActivity+FragmentTabHost+Fragement模式
//定义FragmentTabHost对象
private FragmentTabHost mTabHost;
//定义数组来存放Fragment界面
private static Class<?>[] fragmentArray;
//定义Tab选项卡文字、图片
private static final int[] aiTabWidgetIcon;
private static final int[] astrTabWidgetTitle;
//静态代码块实例化Fragment数组、Tab选项卡文字、图片
static
{
aiTabWidgetIcon = new int[]{R.drawable.icon_eth, R.drawable.icon_wifi, R.drawable.icon_3g, R.drawable.icon_vpn, R.drawable.icon_bluetooth};
astrTabWidgetTitle = new int[]{R.string.STR_LAN, R.string.STR_WIRELESS, R.string.STR_3G, R.string.STR_VPN, R.string.STR_BLUETOOTH};
fragmentArray = new Class<?>[]{EthernetFragment.class, WifiFragment.class, Net3gFragment.class, VpnFragment.class, BluetoothFragment.class};
}
//实例化TabHost对象,得到TabHost
mTabHost = (FragmentTabHost) findViewById(android.R.id.tabhost);
mTabHost.setup(this, getSupportFragmentManager(), R.id.realtabcontent);
//得到fragment的个数
int count = fragmentArray.length;
//为每一个Tab按钮设置图标、文字和内容
for (int i = 0; i < count; i++)
{
View view = LayoutInflater.from(this).inflate(R.layout.tabwidget_style, null);
view.setLayoutParams(new LayoutParams(getWindowManager().getDefaultDisplay().getWidth() /aiTabWidgetIcon.length, 60));
TextView textview = (TextView) view.findViewById(R.id.tabwidget_name);
ImageView icon = (ImageView) view.findViewById(R.id.tabwidget_icon);
textview.setText(astrTabWidgetTitle[i]);
icon.setBackgroundResource(aiTabWidgetIcon[i]);
//设置每个Tab按钮背景,其中背景根据按钮不同状态相应变化
view.setBackgroundResource(R.drawable.tabwidget_selector);
//创建TabHost.TabSpec用来管理Tag、Content、Indicator
TabSpec tabSpec = mTabHost.newTabSpec(getString(astrTabWidgetTitle[i])).setIndicator(view);
//将Tab按钮添加进Tab选项卡中
mTabHost.addTab(tabSpec, fragmentArray[i], null);
}
TabHost是整个Tab的容器,包括两部分---TabWidget和FrameLayout;TabWidget就是每个tab的标签,FrameLayout则是tab内容。
//设置TabHost的背景
mTabHost.getTabWidget().setBackgroundResource(R.drawable.tabwidget_bg);
//按键事件
@Override
public boolean onKeyDown(int keyCode, KeyEvent event)
{
//获取当前的Tab索引
int iCurTab = mTabHost.getCurrentTab();
//根据当前Tab的tag得到当前Fragment
Fragment curFg = getSupportFragmentManager().findFragmentByTag(mTabHost.getCurrentTabTag());
if (curFg instanceof OnkeyDownListener)
{
if (((OnkeyDownListener) curFg).onKeyDown(keyCode, event))
return true;
}
switch (keyCode)
{
case KEY_LEFT://向左切换
Log.d(TAG, " KEY_LEFT iCurTab = " + iCurTab);
if (!moreThanLastKeyTime(300))
{
return true;
}
if (iCurTab > 0)
{//根据index设置当前显示哪个Tab(也可以根据tag设置当前显示哪个Tab,调用setCurrentTabByTag方法)
mTabHost.setCurrentTab(iCurTab - 1);
}
else if (iCurTab == 0)
{
mTabHost.setCurrentTab(fragmentArray.length - 1);
}
return true;
case KEY_RIGHT://向右切换
Log.d(TAG, " KEY_RIGHT iCurTab = " + iCurTab);
if (moreThanLastKeyTime(300))
{
return true;
}
if (iCurTab < (fragmentArray.length - 1))
{
mTabHost.setCurrentTab(iCurTab + 1);
}
else if (iCurTab == (fragmentArray.length - 1))
{
mTabHost.setCurrentTab(0);
}
return true;
}
Log.d(TAG, "exec onKeyDown " + keyCode);
return super.onKeyDown(keyCode, event);
}
相关xml布局
activity_network_setting.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/settings_background" >
<com.john.alien.uistyle.TextImageView
android:id="@+id/network_header"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="@dimen/menu_beyond_top_item"
android:layout_marginLeft="@dimen/menu_title_margin_left"
android:layout_marginTop="@dimen/menu_title_margin_top"
android:focusable="false"
android:gravity="center_vertical" />
<android.support.v4.app.FragmentTabHost
android:id="@android:id/tabhost"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/network_header"
android:layout_marginLeft="15dp"
android:layout_marginRight="15dp"
android:focusable="false" >
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_weight="0"
android:focusable="false" />
</android.support.v4.app.FragmentTabHost>
<FrameLayout
android:id="@+id/realtabcontent"
android:layout_width="match_parent"
android:layout_height="0dip"
android:layout_weight="1" />
<TextView
android:id="@+id/server_status"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_marginBottom="10dp"
android:layout_marginLeft="115dp"
android:textSize="@dimen/text_size_normal"
android:visibility="invisible" />
</RelativeLayout>
tabwidget_style.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="60dp"
android:gravity="center"
android:orientation="horizontal"
android:paddingLeft="5dp"
android:paddingRight="5dp" >
<ImageView
android:id="@+id/tabwidget_icon"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_marginRight="10dp"
android:layout_marginLeft="10dp"
android:gravity="center"/>
<TextView
android:id="@+id/tabwidget_name"
android:layout_width="match_parent"
android:layout_height="60dp"
android:ellipsize="marquee"
android:singleLine="true"
android:gravity="center_vertical|left"
android:textSize="@dimen/text_size_normal" />
</LinearLayout>
CustomFragmentImpl.java
public class CustomFragmentImpl
{
public static interface OnkeyDownListener
{
public boolean onKeyDown(int keyCode, KeyEvent event);
}
public static interface WindowFocusChangeListener
{
public void onWindowFocusChanged(boolean hasFocus);
}
}
最后,每个Fragment需要实现CustomFragmentImpl.OnkeyDownListener接口