2010-08-21 6 views
6

Tôi đang cố gắng tạo các tab nhỏ hơn trong Android - nhưng dường như tôi không thể làm việc này vì tất cả điều đó xảy ra khi tôi tạo tab nhỏ hơn là tab hiển thị tab lớn hơn - nhưng mà không có một drawable.Tạo các tab nhỏ hơn trong Android

Đây là mã bố cục của tôi cho các tab ngay bây giờ - nhưng chiều cao không phải là do một số lý do - nó chỉ đi đến chiều cao bố cục thông thường của 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> 

Nó sẽ được tuyệt vời nếu ai đó có thể giúp tôi tạo ra một cái gì đó giống như ứng dụng Facebook - Tôi nghĩ rằng trông thật sự sạch sẽ và tôi rất thích để thực hiện một cái gì đó giống như nó:

Trả lời

7

Vâng điều này là xa phức tạp hơn tôi nghĩ nó nên được nhưng, tuy nhiên, điều này sẽ giúp bạn có được một triển khai cơ bản về sự xuất hiện mà bạn muốn ...

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); 

Điều này sẽ không tính đến biên giới tab hoặc khoảng cách giữa các đường n yếu tố mặc dù. Bạn cũng cần định nghĩa danh sách trạng thái màu như sau trong thư mục ./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> 

Hy vọng điều đó sẽ hữu ích.

+0

Chà, điều này chắc chắn là nhiều hơn tôi đang tìm kiếm, tôi đoán tôi sẽ chấp nhận câu trả lời này cho đến khi ai đó đăng một cách chắc chắn hơn. – hwrdprkns

+0

STATE_SELECTED và STATE_UNSELECTED được cho là id của cái gì? – pakore

+1

@pakore - Xin lỗi vì bỏ qua những định nghĩa này. STATE_SELECTED được định nghĩa là bằng android.R.attr.state_selected. STATE_UNSELECTED bằng STATE_SELECTED * -1. – Woody

3

Thấy điều này trong một diễn đàn khác, nhưng tôi đã đoán là tôi đã vượt qua nó ở đây.


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

Tôi đã thử công việc này – Federico