底部导航用FragmentTableHost实现

来源:互联网 发布:java split 去除空格 编辑:程序博客网 时间:2024/06/05 17:22
public class MainActivity extends FragmentActivity {/** * FragmentTabhost */private FragmentTabHost mTabHost;/** * 布局填充器 */private LayoutInflater mLayoutInflater;/** * Fragment数组界面 *///定义一个类型是Class的数组,用来存放所有fragment.class页面;private Class mFragmentArray[] = { Fragment1.class, Fragment2.class,Fragment3.class, Fragment4.class};/** * 存放图片数组 *///定义一个Int型的数组,用来存放四个按钮的图标(资源是drawable中的xml文件,包含了不同状态下的图片);private int mImageArray[] = { R.drawable.tab_home_btn,R.drawable.tab_message_btn, R.drawable.tab_selfinfo_btn,R.drawable.tab_square_btn};/** * 选修卡文字 *///四个菜单名字--(即tag值)private String mTextArray[] = { "资讯", "问答", "聊天室", "我的" };/** */public void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);requestWindowFeature(Window.FEATURE_NO_TITLE);setContentView(R.layout.activity_main);//初始化控件;initView();}/** * 初始化组件 */private void initView() {mLayoutInflater = LayoutInflater.from(this);// 找到TabHostmTabHost = (FragmentTabHost) findViewById(android.R.id.tabhost);//第一个参数:上下文,第二个参数:fragment管理器,第三个参数:真正的内容存放位置--默认R.id.realtabcontent;mTabHost.setup(this, getSupportFragmentManager(), R.id.realtabcontent);// 得到fragment的个数,即tabSpec的个数;int count = mFragmentArray.length;for (int i = 0; i < count; i++) {// 新建一个tabSpec并给每个tabSpec按钮设置文字和视图;TabSpec tabSpec = mTabHost.newTabSpec(mTextArray[i]).setIndicator(getTabItemView(i));//为指示器设置视图// 将tabSpec关联到fragment;mTabHost.addTab(tabSpec, mFragmentArray[i], null);// 设置tabSpec默认的背景颜色;mTabHost.getTabWidget().getChildAt(i).setBackgroundResource(R.drawable.selector_tab_background);}}/** * 给每个Tab按钮设置图标和文字 */private View getTabItemView(int index) {View view = mLayoutInflater.inflate(R.layout.tab_item_view, null);ImageView imageView = (ImageView) view.findViewById(R.id.imageview);TextView textView = (TextView) view.findViewById(R.id.textview);imageView.setImageResource(mImageArray[index]);textView.setText(mTextArray[index]);return view;}}

0 0