如何在android中更改选项卡指示器颜色

时间:2015-02-22 12:30:05

标签: android android-tabhost indicator

我想更改android中的标签指示颜色。 android app中标签主机的默认颜色为蓝色。我想换成其他颜色。请帮帮我。

我的代码如下:

`

<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_marginBottom="-4dp"
        android:layout_weight="0" />
    <View 
        android:layout_width="fill_parent"
        android:background="@color/btn_color"
        android:layout_height="5dp"/>

    <FrameLayout
        android:id="@android:id/tabcontent"
        android:layout_width="fill_parent"
        android:layout_height="0dip"
        android:layout_weight="1" />

    <Button
        android:id="@+id/buttonBookConfirm"
        android:layout_width="fill_parent"
        android:layout_height="35dp"
        android:textColor="@color/btn_text"
        android:background="@color/btn_color"
        android:layout_alignParentBottom="true"
        android:text="@string/btn_text" />
</LinearLayout>

`

我的java代码如下: ` 公共类TabHostActivity扩展TabActivity {     /** 在第一次创建活动时调用。 * /

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_tab_host);

    Resources res = getResources(); // Resource object to get Drawables
    TabHost tabHost = getTabHost(); // The activity TabHost
    TabHost.TabSpec spec; // Reusable TabSpec for each tab
    Intent intent; // Reusable Intent for each tab
    // Create an Intent to launch an Activity for the tab (to be reused)
    intent = new Intent().setClass(TabHostActivity.this,HomeActivity.class);
    spec = tabHost.newTabSpec("home")
            .setIndicator("", res.getDrawable(R.drawable.plays))
            .setContent(intent);
    tabHost.addTab(spec);

    // Do the same for the other tabs

    intent = new Intent().setClass(TabHostActivity.this, AboutActivity.class);
    spec = tabHost.newTabSpec("about")
            .setIndicator("", res.getDrawable(R.drawable.movies))
            .setContent(intent);
    tabHost.addTab(spec);


    intent = new Intent().setClass(TabHostActivity.this, ContactActivity.class);
    spec = tabHost
            .newTabSpec("contact")
            .setIndicator("",
                    res.getDrawable(R.drawable.events))
            .setContent(intent);
    tabHost.addTab(spec);

    //set tab which one you want open first time 0 or 1 or 2
    tabHost.setCurrentTab(0);

}`

我附上了相同的图像。我只想更改所选标签下方的蓝色线条。


enter image description here

4 个答案:

答案 0 :(得分:2)

android:setLeftStripDrawable="@color/your_custom_color

在red-&gt; values文件夹中创建自定义颜色资源,并将其引用到

答案 1 :(得分:1)

你应该看看docs for TabWidget。您必须使用android:tabStripLeftandroid:tabStripRight XML属性来更改背景颜色。

最简单的方法可能是使用您想要的颜色创建一个1px x 1px png并将其放在项目的drawable assets目录中,然后使用它。

答案 2 :(得分:1)

只需使用app:tabIndicatorColor="@color/your color"

即可

答案 3 :(得分:0)

您可以使用以下程序以编程方式实现:

TabLayout tabLayout = findViewById(R.id.YOUR_TAB_LAYOUT);

tabLayout.setSelectedTabIndicatorColor(YOUR_COLOR));