1

我一直在嘗試更改主題爲TabHost。到目前爲止,我已經得到了到這裏:如何將TabHost的主題從Holo.Light更改爲Dark主題

Light Tabhost Theme

我已經使用以下XML來實現這一點:

<TabHost 
android:id="@android:id/tabhost" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" > 

<LinearLayout 
android:id="@+id/signupLinearLayout" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
android:orientation="vertical" > 

    <TabWidget 
    android:id="@android:id/tabs" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:layout_gravity="center" 
    android:layout_weight="0" 
    android:gravity="center" 
    android:orientation="horizontal" /> 

     <FrameLayout 
     android:id="@android:id/tabcontent" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_weight="0" > 

      <ScrollView 
      android:id="@+id/scrollView02" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" > 
      </ScrollView> 

      <ScrollView 
      android:id="@+id/scrollView01" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" > 
      </ScrollView>    
     </FrameLayout> 
</LinearLayout> 

MainActivity.java

ContextThemeWrapper wrapper = new ContextThemeWrapper(
ActivityMain.this, 
android.R.style.Theme_Holo_Light); 

final LayoutInflater inflater = (LayoutInflater) wrapper 
    .getSystemService(LAYOUT_INFLATER_SERVICE);        

dialog = new Dialog(wrapper); 
dialog 
    .requestWindowFeature(Window.FEATURE_NO_TITLE); 
dialog 
    .setContentView(R.layout.dialog_layout); 

TabHost tabs = (TabHost) dialog 
    .findViewById(android.R.id.tabhost); 
tabs.setup(); 
tabs.setCurrentTab(0); 

TabSpec tspec1 = tabs.newTabSpec("Tab1"); 
tspec1.setIndicator("SIGN UP"); 
tspec1.setContent(R.id.scrollView02); 
tabs.addTab(tspec1); 

TabSpec tspec2 = tabs.newTabSpec("Tab2"); 
tspec2.setIndicator("LOG IN"); 
tspec2.setContent(R.id.scrollView01); 
tabs.addTab(tspec2); 

正如我使用Dialog類在對話框中查看和整合TabHost,這就是爲什麼我使用ContextThemeWrapper爲此在Dialog上有一些主題。

現在,我的問題是,我該如何將Holo.Light主題更改爲Dark主題。這裏是我想要的圖片: Dark theme for tabhost

我知道android並沒有Holo.Dark這個主題。這僅適用於ActionBars。那麼我怎樣才能實現這個解決方案。

任何形式的幫助將不勝感激。

回答

4

這將工作:

//Changing the tabs background color and text color on the tabs 
for(int i=0;i<tabs.getTabWidget().getChildCount();i++) 
{ 
    tabs.getTabWidget().getChildAt(i).setBackgroundColor(Color.BLACK); 
    TextView tv = (TextView) tabs.getTabWidget().getChildAt(i).findViewById(android.R.id.title); 
        tv.setTextColor(Color.parseColor("#ffffff")); 
} 

而對於指標,有這樣的佈局tabwidget

<LinearLayout 
      android:id="@+id/tab_indicator" 
      android:layout_width="fill_parent" 
      android:layout_height="5dp" 
      android:background="#bdbdbd" > 

      <LinearLayout 
       android:id="@+id/tab_indicator_left" 
       android:layout_width="wrap_content" 
       android:layout_height="5dp" 
       android:layout_weight="1" 
       android:background="#f44b3b" > 
      </LinearLayout> 

      <LinearLayout 
       android:id="@+id/tab_indicator_right" 
       android:layout_width="wrap_content" 
       android:layout_height="5dp" 
       android:layout_weight="1" 
       android:background="#bdbdbd" > 
      </LinearLayout> 
     </LinearLayout> 

下方,改變這樣的基礎上,標籤選擇指標的背景色。

tabindicator1.setBackgroundColor(Color 
          .parseColor("#f44b3b")); 
+0

我會建議保持佈局資源和代碼分開。應該在佈局文件中定義顏色和樣式以獲得更清晰的代碼。 –

1

我會建議使用盡可能多的Android的源成爲可能。在我看來,它確實讓事情變得更加清晰。我在下面添加了一個基本的例子。不是完美的,但比其他任何東西都更接近我能夠比大多數例子更好,更清潔。 https://github.com/android/platform_frameworks_base/tree/master/core/res/res

例如,對於holo主題,請使用它。 https://github.com/android/platform_frameworks_base/blob/master/core/res/res/drawable/tab_indicator_holo.xml 並獲取所有資源並將其放入您的項目中。之後,使用鏈接 http://joshclemm.com/blog/?p=136 並將其修改爲可以隨意使用。

佈局文件

<TabHost 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
xmlns:android="http://schemas.android.com/apk/res/android" 
android:id="@+id/tabHost"> 
<LinearLayout 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
android:orientation="vertical" 
> 
<TabWidget 
    android:id="@android:id/tabs" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:layout_marginLeft="0dip" 
    android:layout_marginRight="0dip" 
    android:background="#000"> 
</TabWidget> 

<FrameLayout 
    android:id="@android:id/tabcontent" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content"> 

</FrameLayout> 

碼 - 同喬什clemm

 mTabHost=(TabHost)getActivity().findViewById(R.id.tabHost); 
    mTabHost.setup(); 
    //mTabHost.getTabWidget().setDividerDrawable(R.drawable.tab_divider); 

    setupTab(new TextView(getActivity()), "Tab 1"); 
    setupTab(new TextView(getActivity()), "Tab 2"); 
    setupTab(new TextView(getActivity()), "Tab 3"); 


private void setupTab(final View view, final String tag) { 
    View tabview = createTabView(mTabHost.getContext(), tag); 
    TabHost.TabSpec setContent = mTabHost.newTabSpec(tag).setIndicator(tabview).setContent(new TabHost.TabContentFactory() { 
     public View createTabContent(String tag) {return view;} 
    }); 
    mTabHost.addTab(setContent); 
} 

private static View createTabView(final Context context, final String text) { 
    View view = LayoutInflater.from(context).inflate(R.layout.tabs_bg, null); 
    TextView tv = (TextView) view.findViewById(R.id.tabsText); 
    tv.setText(text); 
    return view; 
} 

然後tab_bg文件

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
      android:id="@+id/tabsLayout" android:layout_width="fill_parent" 
      android:layout_height="fill_parent" android:background="@drawable/tab_selector" 
      android:padding="10dip" android:gravity="center" android:orientation="vertical"> 
<TextView android:id="@+id/tabsText" android:layout_width="wrap_content" 
      android:layout_height="wrap_content" android:text="Title" 
      android:textSize="15dip" android:textColor="@android:color/white" /> 
</LinearLayout> 
1

在RES /價值/風格s.xml,將父主題更改爲"android:Theme.Holo"而不是"android:Theme.Holo.Light"

這會明顯改變整個應用程序的主題,但您也可以針對不同的活動使用不同的樣式。