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"

这会明显改变整个应用程序的主题,但您也可以针对不同的活动使用不同的样式。