Android 自定义tabhost

来源:互联网 发布:2017中国云计算500强 编辑:程序博客网 时间:2024/05/18 05:56


不用继承TabActivity, 可以放置于界面中的任何位置,以下是具体的做法

xml 中的写法如下:



<TabHost  android:id="@+id/TabHost01" android:layout_width="160px" android:layout_height="400px" android:layout_x="0px" android:layout_y="45px">
<LinearLayout android:layout_width="fill_parent"
android:orientation="vertical" android:layout_height="fill_parent">
<TabWidget android:id="@android:id/tabs"
android:layout_width="160px"
android:layout_height="80px" />
<FrameLayout android:id="@android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout android:id="@+id/LinearLayout1"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TextView android:text="one"
android:id="@+id/TextView01" android:layout_width="wrap_content"
android:layout_height="wrap_content">
</TextView>
</LinearLayout>
<LinearLayout android:id="@+id/LinearLayout2"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView android:text="two"
android:id="@+id/TextView02" android:layout_width="fill_parent"
android:layout_height="wrap_content">
</TextView>
</LinearLayout>
<LinearLayout android:id="@+id/LinearLayout3"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView android:text="three"
android:id="@+id/TextView03" android:layout_width="fill_parent"
android:layout_height="wrap_content">
</TextView>
</LinearLayout>
</FrameLayout>
</LinearLayout>
</TabHost>



java 代码:



TabHost tabHost;
        tabHost = (TabHost) this.findViewById(R.id.TabHost01);
        tabHost.setup();
 
        tabHost.addTab(tabHost.newTabSpec("tab_1")
         .setContent(R.id.LinearLayout1)
         .setIndicator("TAB1",
this.getResources().getDrawable(R.drawable.bar_unselected_3_1)));
        tabHost.addTab(tabHost.newTabSpec("tab_2")
         .setContent(R.id.LinearLayout2).setIndicator("TAB2",
         this.getResources().getDrawable(R.drawable.bar_unselected_3_2)));
        tabHost.addTab(tabHost.newTabSpec("tab_3")
                       .setContent(R.id.LinearLayout3).setIndicator("TAB3",
                        this.getResources().getDrawable(R.drawable.bar_unselected_3_3)));
     tabHost.setCurrentTab(1);

原文链接

原创粉丝点击