1、先建立一个类(IconFramgnetBean),在里面定义出需要的图片,标题,所需要的类(Class
IconFramgnetBean
public class IconFramgnetBean {
private int iconResId;
private String title;
private Class<?> clazz;
public IconFramgnetBean(int iconResId,Class<?> clazz) {
this.iconResId = iconResId;
this.clazz = clazz;
}
public IconFramgnetBean(int iconResId,String title,Class<?> clazz) {
this.iconResId = iconResId;
this.title = title;
this.clazz = clazz;
}
public int getIconResId() {
return iconResId;
}
public void setIconResId(int iconResId) {
this.iconResId = iconResId;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public Class<?> getClazz() {
return clazz;
}
public void setClazz(Class<?> clazz) {
this.clazz = clazz;
}
}
2、建立所需的Fragment:如下
建立所需的Fragment
FragmentHome:
public class FragmentHome extends Fragment {
public static final int RESID = R.drawable.tab_home;
public static final String TITLE = "首页";
@Override
public View onCreateView(LayoutInflater inflater,ViewGroup container,Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_home,container,false);
return view;
}
}
FragmentSay :
public class FragmentSay extends Fragment {
public static final int RESID = R.drawable.tab_say;
public static final String TITLE = "咨询";
@Override
public View onCreateView(LayoutInflater inflater,Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_say,null);
return view;
}
}
FragmentShopCar :
public class FragmentShopCar extends Fragment {
public static final int RESID = R.drawable.tab_shopcar;
public static final String TITLE = "购物车";
@Override
public View onCreateView(LayoutInflater inflater,Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_shopcar,null);
return view;
}
}
FragmentMy :
public class FragmentMy extends Fragment {
public static final int RESID = R.drawable.tab_my;
public static final String TITLE = "我的";
@Override
public View onCreateView(LayoutInflater inflater,Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_my,null);
return view;
}
}
XML文件
fragment_home.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="match_parent"
android:gravity="center"
android:orientation="vertical" >
<ImageView
android:id="@+id/fragment_first"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="@drawable/home"/>
</LinearLayout>
fragment_say.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="match_parent"
android:gravity="center"
android:orientation="vertical" >
<ImageView
android:id="@+id/fragment_second"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="@drawable/say"/>
</LinearLayout>
fragment_shopcar.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="match_parent"
android:gravity="center"
android:orientation="vertical" >
<ImageView
android:id="@+id/fragment_third"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="@drawable/shopcar"
/>
</LinearLayout>
fragment_my.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="match_parent"
android:gravity="center"
android:orientation="vertical" >
<ImageView
android:id="@+id/fragment_fourth"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="@drawable/my"
/>
</LinearLayout>
activity_main.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<FrameLayout
android:id="@+id/realTabContent"
android:layout_width="fill_parent"
android:layout_height="0dip"
android:layout_weight="1" />
<android.support.v4.app.FragmentTabHost
android:id="@android:id/tabhost"
android:layout_width="fill_parent"
android:layout_height="50dip"
android:background="@null" >
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_weight="0" />
</android.support.v4.app.FragmentTabHost>
</LinearLayout>
MainActivity
public class MainActivity extends FragmentActivity {
private List<IconFramgnetBean> beans;
private FragmentTabHost mTabHost;
private Activity getSelf() {
return MainActivity.this;
}
@Override
protected void onCreate(Bundle arg0) {
super.onCreate(arg0);
beans = InitConfig.initIconFragmentBeans();
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main);
mTabHost = (FragmentTabHost) findViewById(android.R.id.tabhost);
mTabHost.setup(getSelf(),getSupportFragmentManager(),R.id.realTabContent);
mTabHost.getTabWidget().setDividerDrawable(null);
// 得到fragment的个数
if (beans != null && !beans.isEmpty()) {
int count = beans.size();
for (int i = 0; i < count; i++) {
IconFramgnetBean iconTextBean = beans.get(i);
TabSpec tabSpec = mTabHost.newTabSpec(iconTextBean.getTitle()).setIndicator(getTabItemView(LayoutInflater.from(getSelf()),iconTextBean));
mTabHost.addTab(tabSpec,iconTextBean.getClazz(),null);
}
mTabHost.setCurrentTab(0);
mTabHost.setOnTabChangedListener(new OnTabChangeListener() {
@Override
public void onTabChanged(String tabId) {
mTabHost.setCurrentTabByTag(tabId);
}
});
}
}
/** * 给Tab按钮设置图标和文字 */
private View getTabItemView(LayoutInflater inflater,IconFramgnetBean iconTextBean) {
ImageView imageView = new ImageView(getSelf());
imageView.setBackgroundResource(iconTextBean.getIconResId());
return imageView;
}
boolean bExit = true;
@Override
public boolean onKeyDown(int keyCode,KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
if (bExit) {
final Timer TTimer = new Timer();
TTimer.schedule(new TimerTask() {
@Override
public void run() {
TTimer.cancel();
bExit = true;
}
},1000);
Toast.makeText(getSelf(),"再按一次退出",Toast.LENGTH_SHORT).show();
bExit = false;
} else {
getSelf().finish();
}
}
return false;
}
}
InitConfig
public class InitConfig {
public static List<IconFramgnetBean> initIconFragmentBeans() {
List<IconFramgnetBean> beans = new ArrayList<IconFramgnetBean>();
beans.add(new IconFramgnetBean(FragmentHome.RESID,FragmentHome.TITLE,FragmentHome.class));
beans.add(new IconFramgnetBean(FragmentSay.RESID,FragmentSay.TITLE,FragmentSay.class));
beans.add(new IconFramgnetBean(FragmentShopCar.RESID,FragmentShopCar.TITLE,FragmentShopCar.class));
beans.add(new IconFramgnetBean(FragmentMy.RESID,FragmentMy.TITLE,FragmentMy.class));
return beans;
}
}
drawable.tab_home
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/shouyeanxia" android:state_selected="true"/>
<item android:drawable="@drawable/shouyeanxia" android:state_pressed="true"/>
<item android:drawable="@drawable/shouye"/>
</selector>