首页 > 代码库 > ======FragmentTabHost======

======FragmentTabHost======

package com.lidroid.xutils.sample;

import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentTabHost;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TabHost;
import android.widget.TextView;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.sample.fragment.BitmapFragment;
import com.lidroid.xutils.sample.fragment.DbFragment;
import com.lidroid.xutils.sample.fragment.HttpFragment;
import com.lidroid.xutils.util.LogUtils;
import com.lidroid.xutils.view.annotation.ContentView;
import com.lidroid.xutils.view.annotation.ViewInject;

@ContentView(R.layout.main)
public class MyActivity extends FragmentActivity {

    @ViewInject(R.id.tabhost)
    private FragmentTabHost mTabHost;

    private Class fragmentArray[] = {
            HttpFragment.class,
            DbFragment.class,
            BitmapFragment.class,};
    private int iconArray[] = {
            R.drawable.icon_http,
            R.drawable.icon_database,
            R.drawable.icon_btimap};
    private String titleArray[] = {
            "Http",
            "db",
            "Bitmap"};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        LogUtils.customTagPrefix = "xUtilsSample"; // 方便调试时过滤 adb logcat 输出
        LogUtils.allowI = false; //关闭 LogUtils.i(...) 的 adb log 输出

        ViewUtils.inject(this);

        setupTabView();
    }

    private void setupTabView() {
        mTabHost.setup(this, getSupportFragmentManager(), R.id.realtabcontent);
        mTabHost.getTabWidget().setDividerDrawable(null);

        int count = fragmentArray.length;

        for (int i = 0; i < count; i++) {
            TabHost.TabSpec tabSpec = mTabHost.newTabSpec(titleArray[i]).setIndicator(getTabItemView(i));
            mTabHost.addTab(tabSpec, fragmentArray[i], null);
            mTabHost.getTabWidget().getChildAt(i).setBackgroundResource(R.drawable.tab_item);
        }

    }

    private View getTabItemView(int index) {
        LayoutInflater layoutInflater = LayoutInflater.from(this);
        View view = layoutInflater.inflate(R.layout.tab_bottom_nav, null);

        ImageView imageView = (ImageView) view.findViewById(R.id.iv_icon);
        imageView.setImageResource(iconArray[index]);

        TextView textView = (TextView) view.findViewById(R.id.tv_icon);
        textView.setText(titleArray[index]);

        return view;
    }
}
<?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" >    </FrameLayout>    <android.support.v4.app.FragmentTabHost        android:id="@+id/tabhost"        android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:background="@drawable/tab_background" >        <TabHost            android:id="@+id/tabs"            android:layout_width="fill_parent"            android:layout_height="wrap_content" >        </TabHost>        <FrameLayout            android:id="@+id/tabcontent"            android:layout_width="fill_parent"            android:layout_height="wrap_content" >        </FrameLayout>    </android.support.v4.app.FragmentTabHost></LinearLayout>
<?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" >    <LinearLayout        android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:gravity="center_horizontal|bottom"        android:orientation="vertical" >        <ImageView            android:id="@+id/iv_icon"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:src=http://www.mamicode.com/"@drawable/icon_http" />>
ico_http.xml
<?xml version="1.0" encoding="utf-8"?><selector xmlns:android="http://schemas.android.com/apk/res/android">    <item android:drawable="@drawable/http_press" android:state_selected="true"></item>    <item android:drawable="@drawable/http"/></selector>
text_view_selector.xml
<?xml version="1.0" encoding="utf-8"?><selector xmlns:android="http://schemas.android.com/apk/res/android">    <item android:state_selected="true" android:color="@color/blue"></item>    <item android:color="@color/white"></item></selector>
tab_item.xml
<?xml version="1.0" encoding="utf-8"?><selector xmlns:android="http://schemas.android.com/apk/res/android">    <item android:drawable="@drawable/tab_item_press" android:state_pressed="true"/>    <item android:drawable="@drawable/tab_item_press" android:state_selected="true"/></selector>


======FragmentTabHost======