2010-01-20 29 views
47

Lớp học của tôi kéo dài kéo dài TabActivityLàm cách nào để thay đổi nền của tiện ích tab Android?

TabHost mTabHost = getTabHost(); 

TabHost.TabSpec tab1 =mTabHost.newTabSpec("tab1"); 
TabHost.TabSpec tab2 =mTabHost.newTabSpec("tab2"); 

tab1 .setIndicator("title tab1"); 
tab2 .setIndicator("title tab2"); 
mTabHost.addTab(tab1);mTabHost.addTab(tab2); 

TabHost.setCurrentTab(0 or 1) 

Ai có thể hướng dẫn cho tôi làm thế nào để thay đổi hình nền hoặc màu sắc của tab đã chọn?

Trả lời

25

Điều gì xảy ra nếu bạn đăng ký sự kiện TabHost.OnTabChanged và gọi mTabHost.getCurrentTabView() để xem Chế độ xem, sau đó xem.setBackgroundResource()?

2

Không this có khắc phục được sự cố của bạn không? Về cơ bản, gọi setBackgroundDrawable trên mỗi chế độ xem tab bằng bộ chọn?

93

này sẽ thiết lập màu sắc tab của bạn:

public static void setTabColor(TabHost tabhost) { 
    for(int i=0;i<tabhost.getTabWidget().getChildCount();i++) { 
     tabhost.getTabWidget().getChildAt(i).setBackgroundColor(Color.parseColor("#FF0000")); //unselected 
    } 
    tabhost.getTabWidget().getChildAt(tabhost.getCurrentTab()).setBackgroundColor(Color.parseColor("#0000FF")); // selected 
} 

và nếu bạn đặt nó trong onTabChangedListener(), nó sẽ giữ cho màu sắc chính xác cho tab được chọn.

+0

Cảm ơn rất nhiều, điều này thực sự giúp tôi. Có cách nào để thực hiện cách tiếp cận này trong XML không? – teoREtik

+1

@teoREtik XML là nội dung tĩnh, chỉ khi hoạt động của bạn được khởi chạy lần đầu tiên (bố cục khởi tạo), vì vậy không. – Blundell

+0

Cảm ơn sự giúp đỡ của bạn .. Câu trả lời này rất hữu ích .. 1 cho rằng .. Chúc mừng .. !! – Aditya1510

36

Như mbaird đã đề cập, giải pháp tốt hơn là sử dụng nền với bộ chọn, vì vậy bạn không phải kiểm tra onTabChanged và cập nhật thủ công. Mã tối thiểu là ở đây:

private void initTabsAppearance(TabWidget tabWidget) { 
    // Change background 
    for(int i=0; i < tabWidget.getChildCount(); i++) 
     tabWidget.getChildAt(i).setBackgroundResource(R.drawable.tab_bg); 
} 

đâu tab_bg là một drawable xml với selector:

<selector xmlns:android="http://schemas.android.com/apk/res/android">  
    <item android:state_selected="true" android:drawable="@drawable/tab_bg_selected" /> 
    <item android:drawable="@drawable/tab_bg_normal" /> 
</selector> 

Đối với Tab tùy biến đầy đủ, tôi sẽ thêm mã để thay đổi tab văn bản phong cách sử dụng theme tùy chọn. Thêm phần này vào styles.xml:

<resources> 

    <style name="MyCustomTheme" parent="@android:style/Theme.Light.NoTitleBar"> 
     <item name="android:tabWidgetStyle">@style/CustomTabWidget</item> 
    </style> 

    <style name="CustomTabWidget" parent="@android:style/Widget.TabWidget"> 
     <item name="android:textAppearance">@style/CustomTabWidgetText</item> 
    </style> 

    <style name="CustomTabWidgetText" parent="@android:style/TextAppearance.Widget.TabWidget"> 
     <item name="android:textSize">12sp</item> 
     <item name="android:textStyle">bold</item> 
    </style> 

</resources> 

Để sử dụng theme này, định nghĩa nó trong AndroidManifest.xml:

<application android:theme="@style/MyCustomTheme"> 

Và bây giờ bạn có widget tab với tùy chỉnh nềntùy chỉnh văn bản phong cách.

0

Tôi đặt thông số 'android: background' trong phần tử TabWidget của XML để cung cấp nền chung cho tất cả các tab.

Sau đó, tôi đã chuyển lượt xem tăng từ một XML khác trong phương thức '.setIndicator'.

View v = LayoutInflater.from(this).inflate(R.layout.tab_widget, null); 
    TextView label = (TextView) v.findViewById(R.id.tabLabel); 
    label.setText("Whatever"); 
tab1 .setContent(v); 

Tôi cảm thấy đó là cách tốt hơn để thực hiện việc này.

2
>  TabHost mTabHost = getTabHost(); 
>  
>  TabHost.TabSpec tab1 =mTabHost.newTabSpec("tab1"); 
>  TabHost.TabSpec tab2 =mTabHost.newTabSpec("tab2"); 
>  
>  tab1.setIndicator("title tab1"); 
>  tab2.setIndicator("title tab2"); 
>  mTabHost.addTab(tab1) ;mTabHost.addTab(tab2); 
>  
>  TabHost.setCurrentTab(0 or 1); 


mTabHost.getTabWidget().getChildAt(0).setBackgroundResource(R.drawable.tab1selector); 

mTabHost.getTabWidget().getChildAt(1).setBackgroundResource(R.drawable.tab2selector);  

mTabHost.getTabWidget().getChildAt(2).setBackgroundResource(R.drawable.tab3selector);  

mTabHost.getTabWidget().getChildAt(3).setBackgroundResource(R.drawable.tab4selector); 

Sử dụng .setBackgroundResource và tabNselector là một XML - tabNselector.xml

<?xml version="1.0" encoding="UTF-8"?> 
<selector xmlns:android="http://schemas.android.com/apk/res/android"> 
    <item android:state_selected="false" android:drawable="@drawable/tabN"/> 
    <item android:state_selected="true" android:drawable="@drawable/tabNsel" /> 
</selector> 
Các vấn đề liên quan