安卓开发-FragmentTabHost+Fragment搭建TabBar界面

使用FragmentTabHost+Fragment搭建TabBar界面

效果图

layout 代码

    <android.support.v4.app.FragmentTabHost
        android:id="@android:id/tabhost"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@android:color/white">
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical">
            <FrameLayout
                android:id="@android:id/tabcontent"
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="1">
            </FrameLayout>
            <TabWidget
                android:id="@android:id/tabs"
                android:layout_width="match_parent"
                android:layout_height="@dimen/TabBarHeight"
                android:layout_gravity="bottom">
            </TabWidget>
        </LinearLayout>
    </android.support.v4.app.FragmentTabHost>

fragment layout代码省略

java代码

初始tabhost的代码 加到onCreate方法中

public void initView(){
        monitorSidebar();
        mTabHost = (FragmentTabHost) findViewById(android.R.id.tabhost);
        mTabHost.setup(this, getSupportFragmentManager(), android.R.id.tabcontent);
        mTabHost.addTab(getTabView(R.string.tab_first, unSelectedTabIcon[0]), IndexOne_Fragment.class, null);
        mTabHost.addTab(getTabView(R.string.tab_second, unSelectedTabIcon[1]), IndexTwo_Fragment.class,null);
        //设置tabs之间的分隔线不显示
        mTabHost.getTabWidget().setShowDividers(LinearLayout.SHOW_DIVIDER_NONE);
        mTabHost.setOnTabChangedListener(new TabHost.OnTabChangeListener() {
            @Override
            public void onTabChanged(String tabId) {
                updateTab(mTabHost);
            }
        });
        initTab(mTabHost);
    }
private int lastChosenTab = -1; //类的私有属性
private final int[] unSelectedTabIcon = {R.drawable.tech,R.drawable.write};//tab未选中状态图标
private final int[] selectedTabIcon = {R.drawable.tech2,R.drawable.write2};//tab选中状态图标

private TabHost.TabSpec getTabView(int textId, int imgId) {
        String text = getResources().getString(textId);
        Drawable drawable = getResources().getDrawable(imgId);
        View tabbar_item = getLayoutInflater().inflate(R.layout.tabbar_item, null);
        TextView tab_title = (TextView) tabbar_item.findViewById(R.id.tab_title);
        ImageView tab_img = (ImageView) tabbar_item.findViewById(R.id.tab_img);
        tab_title.setText(text);
        tab_title.setTextColor(Color.LTGRAY);
        tab_img.setImageDrawable(drawable);
        return mTabHost.newTabSpec(text).setIndicator(tabbar_item);
    }

    private void initTab(TabHost tabHost){
        tabHost.setCurrentTab(0);
        View currTabView = tabHost.getCurrentTabView();
        ImageView imageView = (ImageView) currTabView.findViewById(R.id.tab_img);
        TextView textView = (TextView) currTabView.findViewById(R.id.tab_title);
        imageView.setImageDrawable(getResources().getDrawable(selectedTabIcon[0]));
        textView.setTextColor(Color.BLACK);
        lastChosenTab = 0;
    }


    private void updateTab(TabHost tabHost){
        if(lastChosenTab!=-1){
            View lastTabView = tabHost.getTabWidget().getChildTabViewAt(lastChosenTab);
            ImageView imageView = (ImageView) lastTabView.findViewById(R.id.tab_img);
            TextView textView = (TextView) lastTabView.findViewById(R.id.tab_title);
            imageView.setImageDrawable(getResources().getDrawable(unSelectedTabIcon[lastChosenTab]));
            textView.setTextColor(Color.LTGRAY);
        }
        View currTabView = tabHost.getCurrentTabView();
        int currTabCount = tabHost.getCurrentTab();
        ImageView imageView = (ImageView) currTabView.findViewById(R.id.tab_img);
        TextView textView = (TextView) currTabView.findViewById(R.id.tab_title);
        imageView.setImageDrawable(getResources().getDrawable(selectedTabIcon[currTabCount]));
        textView.setTextColor(Color.BLACK);
        lastChosenTab = currTabCount;
    }

tabbar_item的layout布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="0dp"
    android:layout_height="30dp"
    android:layout_weight="1"
    android:background="@android:color/white"
    android:paddingTop="5dp"
    android:orientation="vertical">

    <ImageView
        android:id="@+id/tab_img"
        android:layout_width="wrap_content"
        android:layout_height="0dp"
        android:layout_gravity="center_horizontal"
        android:layout_weight="3" />

    <TextView
        android:id="@+id/tab_title"
        android:layout_width="wrap_content"
        android:layout_height="0dp"
        android:layout_gravity="center_horizontal"
        android:layout_weight="2"
        android:textSize="10sp" />
</LinearLayout>
发布了43 篇原创文章 · 获赞 16 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/include_IT_dog/article/details/96639062