如何将TabHost的主题从Holo.Light更改为Dark主题

时间:2021-04-01 23:00:45

I have been trying to change the theme for TabHost. So far I have got till here:

我一直试图改变TabHost的主题。到目前为止,我到这里为止:

如何将TabHost的主题从Holo.Light更改为Dark主题

I have achieved this by using the following xml:

我通过使用以下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>

My MainActivity.java:

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

As I'm using Dialog class for the view and integrating TabHost inside the dialog, that's why I'm using ContextThemeWrapper for this to have some theme on the Dialog.

由于我正在使用Dialog类进行视图并在对话框中集成TabHost,这就是为什么我使用ContextThemeWrapper为此在Dialog上有一些主题。

Now, my question is that how can I change the Holo.Light theme to Dark theme. Here is the picture what I want: 如何将TabHost的主题从Holo.Light更改为Dark主题

现在,我的问题是如何将Holo.Light主题更改为Dark主题。这是我想要的图片:

I know that android does not have Holo.Dark theme as of now. That is only available for ActionBars. So how can I achieve this solution.

我知道android目前还没有Holo.Dark主题。这仅适用于ActionBars。那么我该如何实现这个解决方案呢。

Any kind of help will be appreciated.

任何形式的帮助将不胜感激。

4 个解决方案

#1


4  

This will work:

这将有效:

//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"));
} 

And for the indicator, have a layout like this beneath tabwidget

对于指标,在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>

And change the background color of indicator like this based on the tab selection.

并根据选项卡选项更改此指示器的背景颜色。

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

#2


2  

See the link it will helpful How to change default color to Tab Host

请参阅它将有用的链接如何将默认颜色更改为选项卡主机

and also refer this it will helpful

并参考这将有所帮助

http://joshclemm.com/blog/?p=136

http://joshclemm.com/blog/?p=136

#3


1  

I would suggest using as much of android's source as possible. It really makes things cleaner in my opinion. I added a basic example of what I used below. Not perfect but closer than anything else I was able to fine and cleaner than most examples. https://github.com/android/platform_frameworks_base/tree/master/core/res/res

我建议尽可能多地使用android的源代码。在我看来,这真的让事情变得更加清洁。我添加了一个我在下面使用的基本示例。不完美但比其他任何东西更接近我能够比大多数例子更精细和更清洁。 https://github.com/android/platform_frameworks_base/tree/master/core/res/res

For instance, for the holo theme, use this. https://github.com/android/platform_frameworks_base/blob/master/core/res/res/drawable/tab_indicator_holo.xml and get all the resources and put them into your project. After that, use the link http://joshclemm.com/blog/?p=136 and modify it to work as you want.

例如,对于holo主题,请使用此。 https://github.com/android/platform_frameworks_base/blob/master/core/res/res/drawable/tab_indicator_holo.xml并获取所有资源并将它们放入您的项目中。之后,使用链接http://joshclemm.com/blog/?p=136并根据需要对其进行修改。

Your Layout file

您的布局文件

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

Code - same as josh clemm

代码 - 与josh 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;
}

And then tab_bg file

然后是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>

#4


1  

In res/values/styles.xml, change the theme parent to "android:Theme.Holo" instead of "android:Theme.Holo.Light"

在res / values / styles.xml中,将主题父级更改为“android:Theme.Holo”而不是“android:Theme.Holo.Light”

This will change the entire app's theme obviously, but you can also use different styles for different activities.

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

#1


4  

This will work:

这将有效:

//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"));
} 

And for the indicator, have a layout like this beneath tabwidget

对于指标,在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>

And change the background color of indicator like this based on the tab selection.

并根据选项卡选项更改此指示器的背景颜色。

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

#2


2  

See the link it will helpful How to change default color to Tab Host

请参阅它将有用的链接如何将默认颜色更改为选项卡主机

and also refer this it will helpful

并参考这将有所帮助

http://joshclemm.com/blog/?p=136

http://joshclemm.com/blog/?p=136

#3


1  

I would suggest using as much of android's source as possible. It really makes things cleaner in my opinion. I added a basic example of what I used below. Not perfect but closer than anything else I was able to fine and cleaner than most examples. https://github.com/android/platform_frameworks_base/tree/master/core/res/res

我建议尽可能多地使用android的源代码。在我看来,这真的让事情变得更加清洁。我添加了一个我在下面使用的基本示例。不完美但比其他任何东西更接近我能够比大多数例子更精细和更清洁。 https://github.com/android/platform_frameworks_base/tree/master/core/res/res

For instance, for the holo theme, use this. https://github.com/android/platform_frameworks_base/blob/master/core/res/res/drawable/tab_indicator_holo.xml and get all the resources and put them into your project. After that, use the link http://joshclemm.com/blog/?p=136 and modify it to work as you want.

例如,对于holo主题,请使用此。 https://github.com/android/platform_frameworks_base/blob/master/core/res/res/drawable/tab_indicator_holo.xml并获取所有资源并将它们放入您的项目中。之后,使用链接http://joshclemm.com/blog/?p=136并根据需要对其进行修改。

Your Layout file

您的布局文件

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

Code - same as josh clemm

代码 - 与josh 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;
}

And then tab_bg file

然后是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>

#4


1  

In res/values/styles.xml, change the theme parent to "android:Theme.Holo" instead of "android:Theme.Holo.Light"

在res / values / styles.xml中,将主题父级更改为“android:Theme.Holo”而不是“android:Theme.Holo.Light”

This will change the entire app's theme obviously, but you can also use different styles for different activities.

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