修鍊-------------Android TabHost,TabWidget選項卡總結

來源:互聯網
上載者:User

標籤:

修鍊-------------Android TabHost,TabWidget選項卡總結Android之TabHostTabHost,個人理解為選項卡的容器,是一種特殊的FrameLayout布局(幀布局)根據SDK文檔,Container for a tabbed window view. This object holds two children: a set of tab labels that the user clicks to select a specific tab, and a FrameLayout object that displays the contents of that page. The individual elements are typically controlled using this container object, rather than setting values on the child elements themselves。TabHost主要由兩部分組成,標籤和內容,其中內容是一個FrameLayout,當使用者單擊不同的標籤可以顯示不同的內容。使用標籤可以達到分頁的效果,是頁面的內容更加豐富,更加具有親和力,當然與此同時,也會增加頁面的複雜程度.一個簡單的TabHost布局1.首先繼承TabActivity2.通過TabActivity的getTabHost()方法得到一個TabHost對象3.定義選項卡的內容(是一個FrameLayout的對象),並與TabHost綁定起來可以通過一下語句綁定TabHost容器的內容LayoutInflater.from(this).inflate(R.layout.main, tabHost.getTabContentView(), true);4.添加選項卡及設定選項的標題及內容我們知道添加選項卡需要指定一個TabSpec對象,通過TabHost的newTabSpec(選項卡的標識)可以得到,並且可以設定選項卡的標題(可以設定圖片),並且設定選項卡內容,如  tabHost.addTab(tabHost.newTabSpec("tab01")  .setIndicator("標籤1",getResources().getDrawable(R.drawable.icon))  .setContent(R.id.tab01));...: 源碼: tabtest01.zip (43.15 KB, 下載次數: 4) 初學者注意了:如果在 繼承了TabActivity的類中設定了,setContentView(R.layout.main),則有可能導致錯誤,原因可能是因為main布局檔案設定不正確(下面有詳解),解決辦法是建議先刪除此行上面的例子中TabHost只是與一個版面配置容器綁定,也就是說各個選項卡的內容是寫在一個布局檔案中的,然後通過不同的id來區分各個選項卡的內容.如果選項卡的個數過多,或者每個選項卡的布局比較複雜的話,勢必會使版面配置容器顯得臃腫而且可讀性比較差,不利於後期的維護。Android中提供了我們還可以通過setContent(Intent intent)來指定每個選項卡的內容來源碼: 有時候我們需要將選項卡的標題設定的更加個人化,雖然我們知道了setIndicator()方法可以設定選項卡的標題的時候可以指定圖片,但都是圖片在下,文字在圖片上方,我們能不能設定成文字在圖片下方,或者文字在圖片右邊呢?當然可以,android中的setIndicator()方法總共有如下三種形式:TabHost.TabSpec         setIndicator(CharSequence label) TabHost.TabSpec         setIndicator(CharSequence label, Drawable icon) TabHost.TabSpec         setIndicator(View view) 前兩種就不說了,想必現在大家都熟悉了。主要說說第三種,第三種中,方法的參數是一個View,想必大家想在已經知道一些蹊蹺了吧. 好的,廢話不多說,直接上源碼源碼: : tabtest03.zip (51.65 KB, 下載次數: 3) Android之TabWidget上面基本上已經說明了android中TabHost的大部分用法,但還不夠。在開發中,我們有時需要更加實用的TabHost,雖然我們已經可以隨心所欲的設定TabHost,但我們一定會發現,在上面的例子中,Label都是在上面,那怎麼才能讓Label在下面呢?下面介紹TabWidget根據SDK文檔,Displays a list of tab labels representing each page in the parent‘s tab collection. The container object for this widget is TabHost. When the user selects a tab, this object sends a message to the parent container, TabHost, to tell it to switch the displayed page. You typically won‘t use many methods directly on this object. The container TabHost is used to add labels, add the callback handler, and manage callbacks. You might call this object to iterate the list of tabs, or to tweak the layout of the tab list, but most methods should be called on the containing TabHost object.我們可以粗略的理解為TabWidget就是Tab的一個集合,也就是Label欄.根據上面的初學者注意:不要隨便在設定setContentView(R.layout.main)中,這條語句可能引起錯誤.如果設定這條語句的話,那麼對main布局檔案就要正確的配置.怎麼正確定義main.xml布局檔案呢?首先我們需要搞清楚TabHost,TabWidget,FrameLayout之間的關係,我們知道TabHost好比一個選項卡的容器,包括多個選項卡和選項卡的內容,其中選項卡的內容是一個FrameLayout容器,TabWidget可以理解為選項卡欄.正確的main.xml檔案應該包含這三個組件TabHost,TabWidget,FrameLayout並且這三個組件的id 必須為@android:id/tabhost,@android:id/tabs,@android:id/tabcontent如下,<?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">    <LinearLayout           android:orientation="vertical"           android:layout_width="fill_parent"           android:layout_height="fill_parent">                      <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_content"         android:padding="5dp" />           </LinearLayout>    </TabHost>: 使用TabWidget選項卡欄在上方源碼: tabtest04.zip (53.09 KB, 下載次數: 1) 如果想要讓選項卡欄在下方,那麼只需要交換TabWidget和FrameLayout的位置即可,如<?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">    <LinearLayout           android:orientation="vertical"           android:layout_width="fill_parent"           android:layout_height="fill_parent">                       <FrameLayout                android:id="@android:id/tabcontent"         android:layout_width="fill_parent"         android:layout_height="fill_parent"         android:layout_weight="1.0"         android:padding="5dp" />                      <TabWidget                     android:id="@android:id/tabs"                   android:layout_width="fill_parent"                    android:layout_height="wrap_content">           </TabWidget>           </LinearLayout>    </TabHost>: 使用TabWidget選項卡欄在下方源碼: tabtest05.zip (150.63 KB, 下載次數: 7) 注意了,FrameLayout中多了一行android:layout_weight="1.0"語句這個屬性代表的是比重,android中所有的組件預設這個屬性都是為0,意味著他們只在螢幕上顯示他們需要空間的大小。如果設定了該屬性,activity將根據設定的值按比例來劃分空間的大小舉個例子,如果acitivity中有三個組件,假設螢幕高為90cm,三個組件如果設定了該屬性為0的話,那麼他們會以正常的方式顯示在螢幕上。如果第一個組件設定組件設定為0,其餘兩個組件設定為1,那麼這個螢幕將會以0:1:1的方式來顯示,如果第一個組件預設佔10cm,那麼其他兩個組件各佔40cm

 

修鍊-------------Android TabHost,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.