Android TabHost使用

private TabHost tabHost;
   
    private int[] layRes = { R.id.t_1, R.id.t_2 , R.id.t_3 };
   
    @Override
    protected void onCreate(Bundle instance)
    {
        super.onCreate(instance);
       
        initResource();
    }

    /**
     * 初始化资源
     *
     * @param
     * @return void
     * @throws
     * @since TianTian
     */
    private void initResource()
    {
        tabHost = super.getTabHost();
        LayoutInflater.from(this).inflate(R.layout.thost, tabHost.getTabContentView(), true);
        /*
         *  循环取出所有布局标记
         */
        for (int x = 0; x < this.layRes.length; x++)
        {
            TabSpec myTab = tabHost.newTabSpec("tab" + x); // 定义TabSpec
            myTab.setIndicator("标签 - " + x); // 设置标签文字
            myTab.setContent(this.layRes[x]); // 设置显示的组件
            this.tabHost.addTab(myTab); // 增加标签
        }
    }

xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent" android:background="@color/coral">

<LinearLayout android:id="@+id/linearLayout1"
android:layout_width="match_parent" android:layout_height="match_parent">

<LinearLayout android:layout_height="match_parent"
android:id="@+id/t_1" android:layout_width="wrap_content">

<ImageView android:layout_height="wrap_content"
android:src="@drawable/jt5" android:id="@+id/imageView1"
android:layout_width="wrap_content"></ImageView>

</LinearLayout>

<LinearLayout android:layout_height="match_parent"
android:id="@+id/t_2" android:layout_width="wrap_content">

<ImageView android:layout_height="wrap_content"
android:src="@drawable/jt6" android:id="@+id/imageView1"
android:layout_width="wrap_content"></ImageView>

</LinearLayout>

<LinearLayout android:layout_height="match_parent"
android:id="@+id/t_3" android:layout_width="wrap_content">

<ImageView android:layout_height="wrap_content"
android:src="@drawable/jt7" android:id="@+id/imageView1"
android:layout_width="wrap_content"></ImageView>

</LinearLayout>

</LinearLayout>

</LinearLayout>

猜你喜欢

转载自mickey-hou.iteye.com/blog/1628061