更改选项卡背景颜色并删除选项卡之间的分隔线 [英] Change tab background color and remove divider line between tabs

查看:24
本文介绍了更改选项卡背景颜色并删除选项卡之间的分隔线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在我的应用程序中显示选项卡,但默认情况下,在 android 中,选项卡之间有这样的分隔线

I want to show tabs in my application but by default in android between tabs there is divider line like this

                            Tab1 | Tab2 | Tab3 |

但我想显示这样的标签

                            Tab1 Tab2 Tab3

所以我想删除两个选项卡之间的分隔线,并且默认情况下选项卡背景颜色为灰色.所以我想把它改成黑色.

So i want to remove the divider line in between two tabs and also by default the tabs background color is gray color. so i want to change this into black color.

请告诉如何删除两个选项卡之间的分隔线并更改选项卡的背景颜色.

Please tell how to remove divider line in between two tabs and change the background color of tabs.

提前致谢.

最好的问候.

推荐答案

使用此方法和 Layout 为选项卡使用您自己的布局.要移除分隔线,只需将背景 9patch 图形替换为您自己的.

Use this method and Layout to use your own layout for the tab. To remove the divider simply replace the background 9patch graphic with your own.

public static View prepareTabView(Context context, String text, Drawable background) {
    View view = LayoutInflater.from(context).inflate(R.layout.fake_native_tab, null);
    TextView tv = (TextView) view.findViewById(R.id.fakeNativeTabTextView);
    tv.setText(text);
    view.setBackgroundDrawable(background);
    return view;
}

fake_native_tab.xml:

fake_native_tab.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/fakeNativeTabLayout" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:gravity="center"
android:orientation="vertical" android:background="@drawable/default_tab_background">
<!--
       You can even define an Icon here (dont forget to set a custom icon in your code for each Tab):
    <ImageView android:id="@+id/fakeNativeTabImageView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" android:src="@drawable/icon" />
-->
    <TextView android:id="@+id/fakeNativeTabTextView"
    android:layout_width="wrap_content" android:layout_height="wrap_content"
    android:textColor="@color/tab_text_color" android:textSize="@dimen/text_size_tiny"
    android:text="Tab" android:ellipsize="marquee" />

</LinearLayout>

用法(在您的 TabActivity 内):

Usage (inside your TabActivity):

/* Create Tabs */
// reusable Tab Spec
TabHost.TabSpec spec;
Intent tabIntent;
tabHost = getTabHost();
Resources res = getResources();

// Tab 1:
tabIntent = new Intent().setClass(this, Favorite.class);
    spec = tabHost.newTabSpec(TAB_SOMETAB).setIndicator(
            prepareTabView(this, (String) getText(R.string.tab_favorite), res
                    .getDrawable(R.drawable.tab_favorite_background), 0)).setContent(tabIntent);
tabHost.addTab(spec);



// Tab 2:
tabIntent = new Intent().setClass(this, History.class);
spec = tabHost.newTabSpec(TAB_SOMEOTHERTAB).setIndicator(
            prepareTabView(this, (String) getText(R.string.tab_history), res
                    .getDrawable(R.drawable.tab_favorite_background), 0)).setContent(tabIntent);
tabHost.addTab(spec);

这篇关于更改选项卡背景颜色并删除选项卡之间的分隔线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆