Android——TabWidget

來源:互聯網
上載者:User

標籤:

1、activity_tabwidget.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="fill_parent"
    android:layout_height="fill_parent" >

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical" >

        <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" >

            <TextView
                android:id="@+id/tb_tv1"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:text="This is the first tab" />

            <TextView
                android:id="@+id/tb_tv2"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:text="This is another tab" />

            <TextView
                android:id="@+id/tb_tv3"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:text="This is the third tab" />
        </FrameLayout>
    </LinearLayout>

</TabHost>

2、TabWidgetActivity.java

public class TabWidgetActivity extends TabActivity {
    // 聲明TabHost對象
    TabHost tabHost;

    @SuppressWarnings("deprecation")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_tabwidget);
        try {

            // 取得TabHost對象
            tabHost = getTabHost();
            /* 為TabHost添加標籤 */
            // 建立一個newTabSpec(newTabSpec)
            // 設定其標籤和表徵圖(setIndicator)
            // 設定內容(setContent)
            tabHost.addTab(tabHost
                    .newTabSpec("tab_test1")
                    .setIndicator("TAB1",
                            getResources().getDrawable(R.drawable.button1))
                    .setContent(R.id.tb_tv1));
            tabHost.addTab(tabHost
                    .newTabSpec("tab_test2")
                    .setIndicator("TAB2",
                            getResources().getDrawable(R.drawable.button2))
                    .setContent(R.id.tb_tv2));
            tabHost.addTab(tabHost.newTabSpec("tab_test3").setIndicator("TAB3",
                    getResources().getDrawable(R.drawable.button3)).setContent(R.id.tb_tv3));
        
        // 設定TabHost的背景顏色
        tabHost.setBackgroundColor(Color.argb(150, 22, 70, 150));
        // 設定TabHost的背景圖片資源
        tabHost.setBackgroundResource(R.drawable.ic_launcher);
        // 設定當前顯示那一個標籤
        tabHost.setCurrentTab(0);
        } catch (Exception e) {
            e.printStackTrace();
        }
        // 標籤切換事件處理
        tabHost.setOnTabChangedListener(new OnTabChangeListener() {

            @Override
            public void onTabChanged(String tabId) {
                Dialog dialog = new AlertDialog.Builder(TabWidgetActivity.this)
                        .setTitle("提示")
                        .setMessage("當前選中" + tabId + "標籤")
                        .setPositiveButton("確定",
                                new DialogInterface.OnClickListener() {

                                    @Override
                                    public void onClick(DialogInterface dialog,
                                            int whichButton) {
                                        dialog.cancel();
                                    }
                                }).create();
                dialog.show();
            }
        });

    }
}

 

Android——TabWidget

聯繫我們

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