设为首页 加入收藏

TOP

android FragmentTabhost导航分页(一)
2019-08-31 00:05:18 】 浏览:59
Tags:android FragmentTabhost 导航

基本模板

public class MainActivity extends FragmentActivity {
    
    private FragmentTabHost mTabHost;
    private LayoutInflater mLayoutInflater;

    private Class mFragmentArray[] = { Fragment1.class, Fragment2.class,
            Fragment3.class, Fragment4.class, Fragment5.class };

    private int mImageArray[] = { R.drawable.tab_home_btn,
            R.drawable.tab_message_btn, R.drawable.tab_selfinfo_btn,
            R.drawable.tab_square_btn, R.drawable.tab_more_btn };

    private String mTextArray[] = { "首页", "消息", "好友", "搜索", "更多" };

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
    }

    private void initView() {

        mLayoutInflater = LayoutInflater.from(this);

        // 找到TabHost
        mTabHost = (FragmentTabHost) findViewById(android.R.id.tabhost);
        mTabHost.setup(this, getSupportFragmentManager(), R.id.realtabcontent);
     mTabHost.getTabWidget().setDividerDrawable(null);//去除分割线
        // 得到fragment的个数
        for (int i = 0; i < mFragmentArray.length; i++) {
            // 给每个Tab按钮设置图标、文字和内容
            TabSpec tabSpec = mTabHost.newTabSpec(mTextArray[i])
                    .setIndicator(getTabItemView(i));
            // 将Tab按钮添加进Tab选项卡中
            mTabHost.addTab(tabSpec, mFragmentArray[i], null);
            // 设置Tab按钮的背景
            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 = view.findViewById(R.id.imageview);
        //设置图片选择器,选中的tab改变图标
        switch (index){
            case 0:imageView.setImageResource(R.drawable.main_bottom_image_selector);break;
            case 1:imageView.setImageResource(R.drawable.main_bottom_image_selector2);break;
            case 2:imageView.setImageResource(R.drawable.main_bottom_image_selector3);break;
            case 3:imageView.setImageResource(R.drawable.main_bottom_image_selector4);break;
            case 4:imageView.setImageResource(R.drawable.main_bottom_image_selector5);break;
        }
        TextView textView = view.findViewById(R.id.textview);
        textView.setText(mTextArray[index]);
        //设置文本选择器,选中的tab文字高亮
        textView.setTextColor(getResources().getColorStateList(R.color.main_bottom_text_selector)); 

    return view;
}
}

acitivity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="fill_parent"
  android:layout_height="fill_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" //必须使用提供的id
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" 
    android:background="@drawable/bg_tabhost_bg">

    <FrameLayout
      android:id="@android:id/tabcontent" //必须使用提供的id
      android:layout_width="0
首页 上一页 1 2 下一页 尾页 1/2/2
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇[Android][Framework]裁剪SystemS.. 下一篇Android项目实战(五十):微信支..

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目