Android:TabHost导航栏

通常情况下,我们都需要用到TabHost做一个导航功能。

这也就需要我们更好的使用TabHost进行各项界面之间的跳转。

首要我们新建一个XML:

<TabHost 
    android:id="@android:id/tabhost" 
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@drawable/first_bg"
    xmlns:android="http://schemas.android.com/apk/res/android">
    
    <LinearLayout 
        android:orientation="vertical"
        android:layout_width="fill_parent" 
        android:layout_height="fill_parent">

        <FrameLayout 
            android:id="@android:id/tabcontent"
            android:layout_width="fill_parent" 
            android:layout_height="0.0dip"
            android:layout_weight="2.0" />
            
        <TabWidget 
            android:id="@android:id/tabs" 
            android:layout_width="fill_parent" 
            android:layout_height="wrap_content"
            android:padding="2dip"
            android:background="@drawable/down_bar"
            android:layout_weight="0.0"/>
            
    </LinearLayout>
    
</TabHost>

MainActivity 我们进行如下操作:

public class MainActivity extends TabActivity{
    private TabHost mTabHost;
    private LayoutInflater mLayoutInflater;
    
    private Class mTabClassArray[] = {
        ClassicMoiveActivity.class,
        HotMovieActivity.class,
        ExpectMovieActivity.class
    };
    
    private int mImageViewArray[] = {
        R.drawable.sutra,
        R.drawable.hot,
        R.drawable.expect
    };
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        initWidget();
    }
    
    
    private void initWidget() {
        mTabHost = getTabHost();
        mLayoutInflater = LayoutInflater.from(this);

        for (int i = 0; i < mTabClassArray.length; i++) {
            TabSpec tabSpec = mTabHost.newTabSpec("").setIndicator(getTabItemView(i)).setContent(getTabItemIntent(i));
            mTabHost.addTab(tabSpec);
            mTabHost.getTabWidget().getChildAt(i)
                    .setBackgroundResource(R.drawable.selector_tab_background);
        }

    }

    private View getTabItemView(int index) {
        View view = mLayoutInflater.inflate(R.layout.tab_item_view, null);
        
        ImageView imageView = (ImageView) view.findViewById(R.id.imageview);
        if (imageView != null) {
            imageView.setImageResource(mImageViewArray[index]);
        }
        return view;
    }

    private Intent getTabItemIntent(int index) {
        Intent intent = new Intent(this, mTabClassArray[index]);
        return intent;
    }
    
}

 

原文地址:https://www.cnblogs.com/gongcb/p/2494491.html