安卓 TabHost

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
MainActivity.java

import android.app.Activity;
import android.app.ActivityGroup;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TabHost;

public class MainActivity extends ActivityGroup {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        TabHost tabHost = (TabHost)findViewById(android.R.id.tabhost);
        // 初始化TabHos容器
        tabHost.setup(this.getLocalActivityManager());
        
        tabHost.addTab(tabHost.newTabSpec("Tab1").setIndicator("音樂",null).setContent(new Intent(this,OneTab.class)));
        tabHost.addTab(tabHost.newTabSpec("Tab2").setIndicator("視頻",null).setContent(R.id.tab2));
        tabHost.addTab(tabHost.newTabSpec("Tab3").setIndicator("小說",null).setContent(new Intent(MainActivity.this,Test.class)));
        tabHost.addTab(tabHost.newTabSpec("Tab4").setIndicator(null,getResources().getDrawable(R.drawable.ic_launcher)).setContent(R.id.tab4));
        

        
    }
}

OneTab.java

import android.app.Activity;
import android.os.Bundle;

public class OneTab extends Activity{

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.onetab);
	}
}

Test.java

import android.app.Activity;
import android.os.Bundle;


public class Test extends Activity {
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		
		super.onCreate(savedInstanceState);
		setContentView(R.layout.test);
	}
}

界面

activity_main.xml

<RelativeLayout 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"
    tools:context="${relativePackage}.${activityClass}" >

    <TabHost
        android:id="@android:id/tabhost"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignParentLeft="true"
        android:layout_centerVertical="true" >

        <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="match_parent"
                android:layout_weight="1" >



                <LinearLayout
                    android:id="@+id/tab2"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                     android:orientation="vertical" >

                    <TextView
                        android:id="@+id/text2"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:text="2222222222222" />
                </LinearLayout>
                
                <LinearLayout
                    android:id="@+id/tab4"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                     android:orientation="vertical" >

                    <TextView
                        android:id="@+id/text4"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:text="2222222222222" />
                </LinearLayout>


            </FrameLayout>
        </LinearLayout>
    </TabHost>



</RelativeLayout>

onetab.xml

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

    <TextView
        android:id="@+id/textView1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:text="第一個tab的佈局"
        android:textColor="#ff0000" />

</LinearLayout>

test.xml

<?xml version="1.0" encoding="utf-8"?>
<ListView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

</ListView>

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.android_9_tabhost"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="19"
        android:targetSdkVersion="19" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        
          <activity
            android:name=".OneTab"
            android:label="@string/app_name" >
        </activity>
        
        <activity
            android:name=".Test"
            android:label="@string/app_name" >
        </activity>
        
    </application>

</manifest>
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

有勇气的牛排

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值