Android控制項之TabHost

來源:互聯網
上載者:User

建一個Android工程tabHost,包名com.test.www

man的布局檔案,加了一些無關的TextView,可把這些TextView去掉

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >
    <!-- 這個跟 text_id一樣-->
    <TextView
            android:id="@+id/tip_id"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="tip"
            android:textColor="#850"
            android:gravity="center"
            android:layout_gravity="center"/>
    <TabHost
        android:id="@android:id/tabhost"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" >
        <!-- 這個是居正中顯示 -->
        <TextView
            android:id="@+id/textRed_id"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="textRed"
            android:textColor="#f00"
            android:gravity="center"
            android:layout_gravity="center"/>
        <!-- 這個是靠邊垂直置中 -->
        <TextView
            android:id="@+id/textGreen_id"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="textGreen"
            android:textColor="#0f0"
            android:layout_gravity="center"/>
        <!-- 這個是靠邊垂直置中 在TabHost上面-->
        <TextView
            android:id="@+id/textBlue_id"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="textBlue"
            android:textColor="#00f"
            />
        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:orientation="vertical" >
            <TextView
            android:id="@+id/text_id"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="text"
            android:textColor="#f50"
            android:gravity="center"
            android:layout_gravity="center"/>
            <TabWidget
                android:id="@android:id/tabs"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content" >
            </TabWidget>
            
            <FrameLayout
                android:id="@android:id/tabcontent"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:padding="5dp" >
            </FrameLayout>
            
        </LinearLayout>
    </TabHost>

</LinearLayout>

下面的這個Activity只有一個TextView,一點擊進入到TabActivity

package com.test.www;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;

public class TableHostActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
//        setContentView(R.layout.main);
        TextView tv = new TextView(this);
        tv.setText("table host!");
        tv.setTextSize(30);
        tv.setOnClickListener(new OnClickListener() {
            
            public void onClick(View v) {
                // TODO Auto-generated method stub
                startActivity(new Intent(TableHostActivity.this, ActivitytableHost.class));
                finish();
            }
        });
        setContentView(tv);
    }
}

下面這個是TabActivity(選項卡的Activity繼承此類),點擊此類中的TextView到另一個介面顯示選項卡的標題,

package com.test.www;

import com.test.www.R;

import android.app.TabActivity;
import android.content.Intent;
import android.content.res.Resources;
import android.os.Bundle;
import android.util.Log;
import android.widget.TabHost;
import android.widget.TabHost.OnTabChangeListener;

public class ActivitytableHost extends TabActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        Resources res = getResources();
        
        TabHost tabHost = getTabHost();
        //spec說明書,投機
        TabHost.TabSpec spec;
        
        Intent intent;
        //indicator指標
        intent = new Intent(this,ActivityInside.class).putExtra("show", "tab 1");  //把值傳給啟動的介面
        //名字;要顯示的字元;設定動畫
        spec = tabHost.newTabSpec("a1").setIndicator("a1",res.getDrawable(R.drawable.c1))
                .setContent(intent);//設定一個Acitivity
        
        tabHost.addTab(spec);
        
        intent = new Intent(this,ActivityInside.class).putExtra("show", "tab 2");
        
        spec = tabHost.newTabSpec("a2").setIndicator("a2",res.getDrawable(R.drawable.c1))
                .setContent(intent);
        
        tabHost.addTab(spec);
        
        intent = new Intent(this,ActivityInside.class).putExtra("show", "tab 3");
        
        spec = tabHost.newTabSpec("a3").setIndicator("a3",res.getDrawable(R.drawable.c1))
                .setContent(intent); //選項卡的名字和表徵圖
        
        tabHost.addTab(spec);
        
        intent = new Intent(this,ActivityInside.class).putExtra("show", "tab 4");
        
        spec = tabHost.newTabSpec("a4").setIndicator("a4",res.getDrawable(R.drawable.c1))
                .setContent(intent);
        
        tabHost.addTab(spec);
        
        tabHost.setCurrentTab(2); //給一個初始值,指向這個位置
        
        tabHost.setOnTabChangedListener(new OnTabChangeListener() {
            
            public void onTabChanged(String tabId) {
                // TODO Auto-generated method stub
               
            }
        });
    }
}
這個是顯示TabHost選項卡的標籤介面,點上面顯示選項卡的標題,程式退出

package com.test.www;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;

public class ActivityInside extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        TextView tv = new TextView(this);
        tv.setOnClickListener(new OnClickListener() {
            
            public void onClick(View v) {
                // TODO Auto-generated method stub
                finish();
            }
        });
        tv.setText(getIntent().getStringExtra("show"));
        setContentView(tv);
    }
}

設定檔

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

    <uses-sdk android:minSdkVersion="4" />

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <activity
            android:name=".TableHostActivity"
            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="ActivitytableHost"></activity>
        <activity android:name="ActivityInside"></activity>
    </application>

</manifest>
運行效果

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.