TabHost

来源:互联网 发布:python统计字母个数 编辑:程序博客网 时间:2024/05/01 07:38

方式一:继承TabActivity

public class MainActivity extends TabActivity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

TabHost tabHost=getTabHost();
tabHost.addTab(tabHost.newTabSpec("tab1").setIndicator("TAB 1").setContent(R.id.textview1)); 
tabHost.addTab(tabHost.newTabSpec("tab2").setIndicator("TAB 2").setContent(R.id.textview2)); 
tabHost.addTab(tabHost.newTabSpec("tab3").setIndicator("TAB 3").setContent(R.id.textview3)); 

xml文件:

这三行是固定写法

android:id="@android:id/tabhost"

android:id="@android:id/tabs"

android:id="@android:id/tabcontent"

<TabHost
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@android:id/tabhost"
    tools:context="com.example.mytab.MainActivity" >
    <LinearLayout 
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        >
        <TabWidget
            android:id="@android:id/tabs"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"  
            ></TabWidget>
        <FrameLayout 
            android:id="@android:id/tabcontent"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" 
            >
            <TextView 
                android:id="@+id/textview1"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="this is content1" 
             />
            <TextView 
                android:id="@+id/textview2"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="this is content2" 
                />
            <TextView 
                android:id="@+id/textview3"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="this is content3" 
                />
        </FrameLayout>
    </LinearLayout>


</TabHost>

方式二:继承Activity

public class MainActivity extends Activity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

TabHost tabHost=(TabHost) findViewById(R.id.mytab);
tabHost.setup();

tabHost.addTab(tabHost.newTabSpec("tab1").setIndicator("TAB 1").setContent(R.id.textview1)); 
tabHost.addTab(tabHost.newTabSpec("tab2").setIndicator("TAB 2").setContent(R.id.textview2)); 
tabHost.addTab(tabHost.newTabSpec("tab3").setIndicator("TAB 3").setContent(R.id.textview3)); 

xml文件为:

android:id="@+id/mytab" (这个属性可以自定义)

android:id="@android:id/tabs"

android:id="@android:id/tabcontent"

<TabHost
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/mytab"
    tools:context="com.example.mytab.MainActivity" >
    <LinearLayout 
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        >
        <TabWidget
            android:id="@android:id/tabs"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"  
            ></TabWidget>
        <FrameLayout 
            android:id="@android:id/tabcontent"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" 
            >
            <TextView 
                android:id="@+id/textview1"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="this is content1" 
             />
            <TextView 
                android:id="@+id/textview2"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="this is content2" 
                />
            <TextView 
                android:id="@+id/textview3"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="this is content3" 
                />
        </FrameLayout>
    </LinearLayout>


</TabHost>


0 0
原创粉丝点击