android TabHost(選項卡)

來源:互聯網
上載者:User

標籤:des   android   style   blog   http   io   color   ar   os   

1:在布局檔案中配置選項卡的內容

<?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"    android:layout_weight="1">    <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"/>        <FrameLayout            android:id="@android:id/tabcontent"            android:layout_width="match_parent"            android:layout_height="match_parent">            <!-- 定義第一個標籤頁的內容 -->            <LinearLayout                android:id="@+id/tab01"                android:orientation="vertical"                android:layout_width="fill_parent"                android:layout_height="fill_parent">                <TextView                    android:layout_width="wrap_content"                    android:layout_height="wrap_content"                    android:text="女兒國國王 - 2012/12/12"                    android:textSize="11pt" />                <TextView                    android:layout_width="wrap_content"                    android:layout_height="wrap_content"                    android:text="東海龍女 - 2012/12/18"                    android:textSize="11pt" />            </LinearLayout>            <!-- 定義第二個標籤頁的內容 -->            <LinearLayout                android:id="@+id/tab02"                android:orientation="vertical"                android:layout_width="fill_parent"                android:layout_height="fill_parent">                <TextView                    android:layout_width="wrap_content"                    android:layout_height="wrap_content"                    android:text="白骨精  - 2012/08/12"                    android:textSize="11pt" />                <TextView                    android:layout_width="wrap_content"                    android:layout_height="wrap_content"                    android:text="蜘蛛精 - 2012/09/20"                    android:textSize="11pt" />            </LinearLayout>            <!-- 定義第三個標籤頁的內容 -->            <LinearLayout                android:id="@+id/tab03"                android:orientation="vertical"                android:layout_width="fill_parent"                android:layout_height="fill_parent"                android:textSize="11pt">                <TextView                    android:layout_width="wrap_content"                    android:layout_height="wrap_content"                    android:text="孫悟空 - 2012/09/19"                    android:textSize="11pt" />                <TextView                    android:layout_width="wrap_content"                    android:layout_height="wrap_content"                    android:text="豬八戒  - 2012/10/12"                    android:textSize="11pt" />                <Button                     android:id="@+id/btn"                    android:layout_width="match_parent"                    android:layout_height="wrap_content"                    android:text="確定"/>            </LinearLayout>        </FrameLayout>    </LinearLayout></TabHost>

響應的代碼

package org.crazyit.ui;import android.app.TabActivity;import android.os.Bundle;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.TabHost;import android.widget.Toast;import android.widget.TabHost.TabSpec;/** * Description: * <br/>site: <a href="http://www.crazyit.org">crazyit.org</a> * <br/>Copyright (C), 2001-2014, Yeeku.H.Lee * <br/>This program is protected by copyright laws. * <br/>Program Name: * <br/>Date: * @author  Yeeku.H.Lee [email protected] * @version  1.0 */public class TabHostTest extends TabActivity{    @Override    public void onCreate(Bundle savedInstanceState)    {        super.onCreate(savedInstanceState);        setContentView(R.layout.main);        // 擷取該Activity裡面的TabHost組件        TabHost tabHost = getTabHost();        // 建立第一個Tab頁        TabSpec tab1 = tabHost.newTabSpec("tab1")            .setIndicator("已接電話") // 設定標題            .setContent(R.id.tab01); //設定內容        // 添加第一個標籤頁        tabHost.addTab(tab1);        TabSpec tab2 = tabHost.newTabSpec("tab2")            // 在標籤標題上放置表徵圖            .setIndicator("呼出電話", getResources()            .getDrawable(R.drawable.ic_launcher))            .setContent(R.id.tab02);        // 添加第二個標籤頁        tabHost.addTab(tab2);        TabSpec tab3 = tabHost.newTabSpec("tab3")            .setIndicator("未接電話")            .setContent(R.id.tab03);        // 添加第三個標籤頁        tabHost.addTab(tab3);        Button btn = (Button)findViewById(R.id.btn);        btn.setOnClickListener(new OnClickListener() {                        @Override            public void onClick(View v) {                Toast.makeText(getApplicationContext(), "toast", Toast.LENGTH_SHORT).show();                            }        });    }}

 

 

2:選項卡的內容通過不同的布局檔案配置

main.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"    android:layout_weight="1">    <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"/>        <FrameLayout            android:id="@android:id/tabcontent"            android:layout_width="match_parent"            android:layout_height="match_parent">        </FrameLayout>    </LinearLayout></TabHost>

設定選項卡的activity

package org.crazyit.intent;import android.app.TabActivity;import android.content.Intent;import android.os.Bundle;import android.widget.TabHost;import android.widget.TabHost.TabSpec;/** * Description: * <br/>site: <a href="http://www.crazyit.org">crazyit.org</a> * <br/>Copyright (C), 2001-2014, Yeeku.H.Lee * <br/>This program is protected by copyright laws. * <br/>Program Name: * <br/>Date: * @author  Yeeku.H.Lee [email protected] * @version  1.0 */public class IntentTab extends TabActivity{    @Override    public void onCreate(Bundle savedInstanceState)    {        super.onCreate(savedInstanceState);        setContentView(R.layout.main);        // 擷取該Activity裡面的TabHost組件        TabHost tabHost = getTabHost();        // 使用Intent添加第一個Tab頁面

     //BacallActivity ,CallActivity,NoCallActitity為三個activity,控制顯示選項卡的內容
        tabHost.addTab(tabHost            .newTabSpec("tab1")            .setIndicator("已接電話",                getResources().getDrawable(R.drawable.ic_launcher))            .setContent(new Intent(this, BeCalledActivity.class)));        // 使用Intent添加第二個Tab頁面        tabHost.addTab(tabHost.newTabSpec("tab1")            .setIndicator("呼出電話")            .setContent(new Intent(this, CalledActivity.class)));        // 使用Intent添加第三個Tab頁面        tabHost.addTab(tabHost.newTabSpec("tab1")            .setIndicator("未接電話")            .setContent(new Intent(this, NoCallActivity.class)));    }}

android TabHost(選項卡)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.