2011-05-25 80 views
0

我只想在我的TabLayout的選項卡中使用文本。不幸的是,默認情況下,製表符非常大,文本非常小。我沒有辦法調整文本的大小或標籤的高度,沒有使用TabSpec.setIndicator(View);這將是非常不可取的,因爲那時我會定義一個特定的佈局,選擇器,選定的圖像,未選中的圖像等等,所以我的選項卡不適合每個設備的外觀和感覺。有沒有一種方便的方式來定製Tab外觀?Android:漂亮的TabLayout?

mTabHost.addTab(mTabHost.newTabSpec("system").setIndicator("My first tab").setContent(R.id.fileBrowserTabHostSystemList)); 
mTabHost.addTab(mTabHost.newTabSpec("recent").setIndicator("My Second tab").setContent(R.id.fileBrowserTabHostRecentList)); 

enter image description here

+3

這是我遇到的最好的例子:http://joshclemm.com/blog/?p=136 – Smith3 2011-05-25 14:58:46

+0

我真的不喜歡這樣做,因爲我想我的選項卡使用本地外觀和感覺。 – ab11 2011-05-25 17:42:48

回答

2

我已成功地改變一種很好的方式的標籤高度:

final int tabChildrenCount = tabWidget.getChildCount(); 
    View currentTab; 
    for (int i = 0; i < tabChildrenCount; i++) { 
     currentTab= tabWidget.getChildAt(i); 
     currentTab.getLayoutParams().height = 32; 
    } 
    tabWidget.getLayoutParams().height = LayoutParams.WRAP_CONTENT; 
    tabWidget.requestLayout(); 

Tabs with smaller height

我也試圖拿到冠軍的高度,在該選項卡並設置標籤的高度:

currentTab.findViewById(android.R.id.title).getHeight(); 
// also tried with getMeasuredHeight() 

但它返回0,因爲它在onCreate()方法中,並且視圖似乎不知道它的大小。 LayoutParams.WRAP_CONTENT對於選項卡高度並不好,因爲它充當FILL_PARENT。

但是,請注意,此解決方案只假定標籤的標題將適合32像素。

仍然最安全的猜測是手動執行佈局(因爲已發佈http://joshclemm.com/blog/?p=136)。從Android存儲庫獲取資源並進行定製:)

+0

我想沒有好的方法去做這件事。感謝您的建議。 – ab11 2011-05-25 17:42:12

+0

ab11,檢查新的解決方案。 – Kamen 2011-05-26 09:04:29

+0

Kamen,謝謝,這很有幫助。 – ab11 2011-05-26 12:30:40