Android之TabHost

来源:互联网 发布:mac rar文件 编辑:程序博客网 时间:2024/06/17 05:36

 TabHost可以实现tab的效果

1、编写tabhost.xml

<?xml version="1.0" encoding="utf-8"?><TabHost xmlns:android="http://schemas.android.com/apk/res/android"    android:id="@android:id/tabhost"    android:layout_width="match_parent"    android:layout_height="match_parent" >    <LinearLayout        android:layout_width="match_parent"        android:layout_height="match_parent"        android:orientation="vertical" >        <FrameLayout            android:id="@android:id/tabcontent" //注意id的写法,表示放置内容            android:layout_width="match_parent"            android:layout_height="match_parent"            android:layout_weight="1" >            <LinearLayout                android:id="@+id/l1"                android:layout_width="match_parent"                android:layout_height="match_parent"                android:orientation="horizontal" >                <TextView                    android:layout_width="match_parent"                    android:layout_height="wrap_content"                    android:text="第一页" />            </LinearLayout>            <LinearLayout                android:id="@+id/l2"                android:layout_width="match_parent"                android:layout_height="match_parent"                android:orientation="horizontal" >                <TextView                    android:layout_width="match_parent"                    android:layout_height="wrap_content"                    android:text="第二页" />            </LinearLayout>            <LinearLayout                android:id="@+id/l3"                android:layout_width="match_parent"                android:layout_height="match_parent"                android:orientation="horizontal" >                <TextView                    android:layout_width="match_parent"                    android:layout_height="wrap_content"                    android:text="第三页" />            </LinearLayout>        </FrameLayout>        <TabWidget            android:id="@android:id/tabs"  //注意id,表示标签            android:layout_width="match_parent"            android:layout_height="wrap_content" />    </LinearLayout></TabHost>


 

2、对应的Java代码

 

package org.zqy.andr;import android.os.Bundle;import android.app.TabActivity;import android.widget.ImageView;import android.widget.TabHost;import android.widget.TabHost.TabSpec;public class TabHostActivity extends TabActivity {private TabHost th;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.tabhost);ImageView img = new ImageView(TabHostActivity.this);img.setImageResource(R.drawable.ic_launcher);th = getTabHost();TabSpec ts1 = th.newTabSpec("tab1").setIndicator("项目一",getResources().getDrawable(R.drawable.ic_launcher)).setContent(R.id.l1);TabSpec ts2 = th.newTabSpec("tab2").setIndicator(img).setContent(R.id.l2);TabSpec ts3 = th.newTabSpec("tab3").setIndicator("项目三",getResources().getDrawable(R.drawable.ic_launcher)).setContent(R.id.l3);th.addTab(ts1);th.addTab(ts2);th.addTab(ts3);th.setCurrentTab(2);//指定默认进入的tab}}


运行结果: