2010-08-21 14 views
6

我想在android中創建更小的選項卡 - 但我似乎無法讓它工作,因爲當我創建一個較小的選項卡時發生的所有事情是它顯示更大的選項卡 - 但沒有可繪製的。在Android中創建更小的選項卡

這是我現在的選項卡布局代碼 - 但由於某種原因,高度沒有包裝 - 它只是去了Android平時的佈局高度。

<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: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" /> 
     <FrameLayout android:id="@android:id/tabcontent" 
      android:layout_width="fill_parent" android:layout_height="fill_parent"> 
     </FrameLayout> 
    </LinearLayout> 
</TabHost> 

這將是巨大的,如果有人可以幫助我建立類似Facebook應用程序 - 我認爲,看起來很乾淨,我很願意來實現類似的東西:

回答

7

嗯,這是遠更復雜的比我想它應該是,但儘管如此,這應該給你你想要的外觀的基本實現...

TabHost    host  = getTabHost(); 
TabSpec    spec  = null; 
TextView   tab1  = null, 
        tab2  = null; 
Intent    intent  = null; 
Resources   resources = getResources(); 
XmlResourceParser parser  = null; 
ColorStateList  text  = null; 
StateListDrawable[] drawables = new StateListDrawable[2]; 
int[]    selected = {STATE_SELECTED}, 
        unselected = {STATE_UNSELECTED}; 
Color    selectedColor = Color.argb(255, 255, 255, 255), 
        defaultColor = Color.argb(255, 119, 119, 119); 

// Load the colour lists. 
parser = resources.getXml(R.color.tab_text); 
text = ColorStateList.createFromXml(getResources(), parser); 

// Add an initial tab. 
...Create Tab Contents Here... 
spec = host.newTabSpec("tab1"); 
tab1 = new TextView(this); 
tab1.setText(R.string.all_tab_title); 
tab1.setGravity(android.view.Gravity.CENTER); 
tab1.setTextSize(18.0f); 
tab1.setTextColor(text); 
spec.setIndicator(tab1); 
spec.setContent(intent); 
host.addTab(spec); 

// Add a second tab. 
...Create Tab Contents Here... 
spec = host.newTabSpec("tab2"); 
tab2 = new TextView(this); 
tab2.setText(R.string.category_tab_title); 
tab2.setGravity(android.view.Gravity.CENTER); 
tab2.setTextSize(18.0f); 
tab2.setTextColor(text); 
spec.setIndicator(tab2); 
spec.setContent(intent); 
host.addTab(spec); 

// Set the background drawable for the tabs and select the first tab. 
drawables[0] = new StateListDrawable(); 
drawables[0].addState(selected, new ColorDrawable(selectedColor)); 
drawables[0].addState(unselected, new ColorDrawable(defaultColor)); 
drawables[1] = new StateListDrawable(); 
drawables[1].addState(selected, new ColorDrawable(selectedColor)); 
drawables[1].addState(unselected, new ColorDrawable(defaultColor)); 
tab1.setBackgroundDrawable(drawables[0]); 
tab2.setBackgroundDrawable(drawables[1]); 
host.setCurrentTab(0); 

這將不考慮選項卡邊框或間距betwee儘管n個元素。您還需要類似於./res/color目錄中的以下顏色狀態列表定義...

<?xml version="1.0" encoding="UTF-8"?> 
<selector xmlns:android="http://schemas.android.com/apk/res/android"> 
    <item android:state_selected="true" android:color="#ff000000" /> 
    <item android:state_selected="false" android:color="#ffaaaaaa" /> 
    <item android:color="#ffffffff"/> 
</selector> 

希望有所幫助。

+0

哇,這絕對比我尋找的要多得多,我想我會接受這個答案,直到有人發佈了更確定的方式。 – hwrdprkns 2010-10-06 13:12:40

+0

STATE_SELECTED和STATE_UNSELECTED應該是id的什麼? – pakore 2010-11-09 14:54:05

+1

@pakore - 忽視這些定義的道歉。 STATE_SELECTED被定義爲等於android.R.attr.state_selected。 STATE_UNSELECTED等於STATE_SELECTED * -1。 – Woody 2010-12-07 12:20:19

3

在另一個論壇上看到了這個,但是我想通過它在這裏。


TabHost th = getTabHost(); 
.... 
// Setup all the tabs -- in my case, with text only -- no icons 
.... 
int iCnt = th.getTabWidget().getChildCount(); 
for(int i=0; i&ltiCnt; i++) 
    th.getTabWidget().getChildAt(i).getLayoutParams().height /= 2; // Or the size desired 
+0

我試過這個並且可以工作 – Federico 2011-12-29 01:05:57

相關問題