安卓進階組件-----選項卡,安卓-----選項卡

來源:互聯網
上載者:User

安卓進階組件-----選項卡,安卓-----選項卡

        選項卡是安卓進階組件中常用到的一種,能夠對頁面進行儉省,比如我們常用的qq介面,連絡人,訊息等功能選項頁其實是在一個activity裡面的,對他們進行切換並沒有導致頁面的重新整理;再如,手機的電話功能包括了連絡人,未接電話,已接電話等,他們都是在一個介面中,是在同一個介面的切頁進行切換。如果把上述放到不同的介面,似乎使用起來會很累贅。所以本節介紹使用選項卡,製作簡單的切頁。

解釋一下選項卡:

        選型卡功能讓新手有點懵,作為新手,大致說說:

如,一個整體的選項卡包括數個切頁,這個整體是TabHost組件,組件內含一個垂直的線性布局,上部分是切網頁標題(TabWidget),下部分是幀布局。我們每次通過標題選擇切頁,都會引起下發的幀布局變化,選擇的幀會出現在最上面,這就是選項卡的原理。在代碼方面,整體稱為TabHost,下方的標題和幀組成TabSpec,增減切頁其實增減的是TabSpec。

1.添加基本布局組件(eclipse拖拽進來會多出幾個線性布局,可以刪除,因為那是幀布局內部的布局,可以不用)

    <TabHost                                                           //此處是TabHost        android:id="@android:id/tabhost"        android:layout_width="match_parent"        android:layout_height="match_parent" >        <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" >            </FrameLayout>        </LinearLayout>    </TabHost>

2.擷取TabHost並且初始化

        private TabHost tabHost;        tabHost =(TabHost)findViewById(android.R.id.tabhost);        tabHost.setup();

3.建立三個布局檔案,這三個布局檔案是加給選項卡的幀容器的,這樣就能實現內容切換

<?xml version="1.0" encoding="utf-8"?><ImageView xmlns:android="http://schemas.android.com/apk/res/android"    android:id="@+id/n"    android:src="@drawable/nature"    android:layout_width="match_parent"    android:layout_height="match_parent" >    </ImageView>

4.給TabHost添加切頁,包括標題和幀。主活動裡面添加布局是通過setContentView()實現初始化,選項卡的添加是在已知布局裡面新增內容,不是布局的初始化,所以我們需要通過布局填充才能實現將定義的三個布局添加到幀中去。

        LayoutInflater inflater = LayoutInflater.from(this);             //獲得布局填充器                inflater.inflate(R.layout.lay1, tabHost.getTabContentView());                inflater.inflate(R.layout.lay2, tabHost.getTabContentView());        inflater.inflate(R.layout.lay3, tabHost.getTabContentView());                tabHost.addTab(tabHost.newTabSpec("tab01").setIndicator("山水").setContent(R.id.n));        tabHost.addTab(tabHost.newTabSpec("tab02").setIndicator("風景").setContent(R.id.s));        tabHost.addTab(tabHost.newTabSpec("tab03").setIndicator("啟動").setContent(R.id.i));

反思:選項卡還是比較簡單實現的,唯一用到一個新手難以理解的布局填充,這還是多看看相關的文章。

請留言,協助改進!

 

聯繫我們

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