android - 如何将标签样式设置为带有彩色文本的浅色主题?

标签 android xml

我正在尝试将选项卡样式设置为浅色主题。给我白色标签。我已经尝试了几种方法,但我无法让这些家伙改变颜色!我可以在 Manifest、TabHost 或 Tab Widget 中分配主题吗?

样式.xml

<style name="SBstyle" parent="@android:style/Theme.Light">

    <item name="android:windowNoTitle">true</item>
    <item name="android:tabWidgetStyle">@style/LightTabWidget</item>
</style>


<style name="LightTabWidget" parent="@android:style/Widget.TabWidget">

<item name="android:textColor">#de6001</item>

那我有 我的 list .xml

<application android:icon="@drawable/icon" android:label="@string/app_name" android:theme="@style/SBstyle">

最后是我的 tab.xml

<?xml version="1.0" encoding="utf-8"?>
 <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"
android:background="#ffffff"
>
<LinearLayout
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:padding="0dp"
    >
    <include layout="@layout/nav_bar" android:layout_height="47dp"
        android:layout_width="fill_parent" android:layout_alignParentTop="true" />
    <TabWidget
        android:id="@android:id/tabs"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:background="#f8a96e"
        android:tabStripEnabled="false"

        />
    <FrameLayout
        android:id="@android:id/tabcontent"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:padding="5dp" />
</LinearLayout>
</TabHost>

感谢您对此提供的任何帮助,谢谢!

最佳答案

如果您使用完全自定义的选项卡,则可以对该选项卡执行任何操作。这是代码……希望对您有所帮助:

    private void initializeTabs(int curTab) {
    this.tabHost = getTabHost();
    tabHost.clearAllTabs();

    TabSpec ts1, ts2, ts3, ts4, ts5;
    // tab separator
    tabHost.getTabWidget().setDividerDrawable(R.drawable.tab_divider);

    ts1 = this.setupTab(new TextView(this), tabHost, R.drawable.browse_tab_normal, 
            mResources.getString(R.string.Browse));

    ts2 = this.setupTab(new TextView(this), tabHost, R.drawable.search_tab_normal, 
            mResources.getString(R.string.Search));

    ts3 = this.setupTab(new TextView(this), tabHost, R.drawable.postad_tab_normal, 
            mResources.getString(R.string.Post));

    ts4 = this.setupTab(new TextView(this), tabHost, R.drawable.watchlist_tab_normal, 
            mResources.getString(R.string.WatchList));

    ts5 = this.setupTab(new TextView(this), tabHost, R.drawable.managead_tab_normal, 
            mResources.getString(R.string.Login));

    // intents
    ts1.setContent(new Intent().setClass(this, BrowseTabActivity.class));
    ts2.setContent(new Intent().setClass(this, SearchTabActivity.class));
    ts3.setContent(new Intent().setClass(this, PostAdTabActivity.class));
    ts4.setContent(new Intent().setClass(this, WatchlistTabActivity.class));
    ts5.setContent(new Intent().setClass(this, LoginTabActivity.class));

    tabHost.addTab(ts1);
    tabHost.addTab(ts2);
    tabHost.addTab(ts3);
    tabHost.addTab(ts4);
    tabHost.addTab(ts5);


    /**
     * Reset the tabs by showing the tab home screen everytime the tab
     * is clicked in any screen other than home screen.
     */
    getTabWidget().getChildAt(0).setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            if (getTabHost().getCurrentTabTag().equals(mTag1) == false) {
                getTabHost().setCurrentTab(0);
            }
            handleTabClicks();
        }
    });
    getTabWidget().getChildAt(2).setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            if (getTabHost().getCurrentTabTag().equals(mTag2) == false) {
                getTabHost().setCurrentTab(1);
            }
            handleTabClicks();
        }
    });
    getTabWidget().getChildAt(4).setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            if (getTabHost().getCurrentTabTag().equals(mTag3) == false) {
                getTabHost().setCurrentTab(2);
            }
            handleTabClicks();
        }
    });
    getTabWidget().getChildAt(6).setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            if (getTabHost().getCurrentTabTag().equals(mTag4) == false) {
                getTabHost().setCurrentTab(3);
            }
            handleTabClicks();
        }
    });

    // Login
    getTabWidget().getChildAt(8).setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            if (getTabHost().getCurrentTabTag().equals(mTag5) == false) {
                getTabHost().setCurrentTab(4);
            }
            handleTabClicks();
        }
    });

    // so that we can programatically switch tabs
    ((ApplicationHelper) getApplication()).setTabHost(tabHost);

    fl = (FrameLayout) findViewById(android.R.id.tabcontent);

    tabHost.setCurrentTab(curTab);

}

我从 onCreate() 调用 initializeTabs()。

setupTab 看起来像这样:

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

private static View createTabView(final Context context, final int imageId, final String text) {
    View view = LayoutInflater.from(context).inflate(R.layout.tab_with_icon, null);
    TextView tv = (TextView) view.findViewById(R.id.tabTitle);
    tv.setText(text);

    ImageView iv = (ImageView) view.findViewById(R.id.iconImage);
    if (iv != null)
        iv.setImageResource(imageId);
    view.setTag(text);
    view.setBackgroundResource(R.drawable.tab_bg_selector);

    // only refresh on watchlist
    if (text.equals(context.getString(R.string.WatchList)))
        refreshTab(context, view);
    else {
        RelativeLayout countLayout = (RelativeLayout) view.findViewById(R.id.countLayout);
        if (countLayout != null)
            countLayout.setVisibility(View.GONE);
    }
    return view;
}

最后,R.layout.tab_with_icon 的 XML:

    <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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_bg_selector" 
    android:orientation="vertical">
    <RelativeLayout android:layout_width="wrap_content" android:id="@+id/relativeLayout1" android:layout_height="wrap_content" android:gravity="center" android:layout_centerInParent="true" android:background="@drawable/tab_bg_selector">
        <ImageView android:layout_width="wrap_content" android:id="@+id/iconImage" android:src="@drawable/watchlist_tab_normal" android:layout_height="wrap_content" android:layout_centerHorizontal="true"></ImageView>
        <TextView android:text="Title" android:layout_width="wrap_content" android:id="@+id/tabTitle" android:layout_height="wrap_content" android:layout_below="@+id/iconImage" android:layout_centerHorizontal="true" android:ellipsize="marquee" android:lines="1" android:maxLines="1" android:scrollHorizontally="true" android:textSize="@dimen/tabTextSize"></TextView>
        <RelativeLayout android:layout_width="wrap_content" android:id="@+id/countLayout" android:layout_height="wrap_content" android:layout_alignRight="@+id/iconImage">
            <ImageView android:layout_width="wrap_content" android:id="@+id/redImage" android:src="@drawable/watchlist_count" android:layout_height="wrap_content"></ImageView>
            <TextView android:text="1" android:textColor="@color/white" android:layout_width="wrap_content" android:id="@+id/countText" android:layout_height="wrap_content" android:layout_centerInParent="true" android:textStyle="bold"></TextView>
        </RelativeLayout>
    </RelativeLayout>
</RelativeLayout>

关于android - 如何将标签样式设置为带有彩色文本的浅色主题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3273103/

相关文章:

Android 应用程序崩溃,在 logcat 中查找原因

c# - 从数据库中解析xml

java - org.xml.sax.SAXParseException : cvc-complex-type. 2.4.c: 匹配的通配符是严格的

android - 没有gradle的缓存版本-不禁用离线模式

java - 尽管 Activity 存在于 AndroidManifest.xml 中,但该 Activity 却不在其中

java - ASCII 字符读取问题 : Euro symbol coming empty

c++ - MSXML DOM : Add namespace declaration to an existing node in a tree

Android Studio 无法解析 ?android : resources

android - 使用多个键搜索和更新 firebase

android - 如何将 LaTex 文本从服务器解码到 Android TextView?