【问题标题】:Change Tab bar selected tab colour in Android在Android中更改标签栏选定的标签颜色
【发布时间】:2015-12-31 03:11:05
【问题描述】:

我使用TabHost 作为标签栏。下面是添加标签的代码:

// Create  Intents to launch an Activity for the tab (to be reused)
intent = new Intent().setClass(this, Tab1.class);

spec = tabHost.newTabSpec("First").setIndicator("",getResources().getDrawable(R.drawable.transaction_refno))
              .setContent(intent);

//Add intent to tab
tabHost.addTab(spec);

/************* TAB2 ************/
intent = new Intent().setClass(this, Tab2.class);
spec = tabHost.newTabSpec("Second").setIndicator("",getResources().getDrawable(R.drawable.transaction_refno))
              .setContent(intent);  
tabHost.addTab(spec);

/************* TAB3 ************/
intent = new Intent().setClass(this, Tab3.class);
spec = tabHost.newTabSpec("Third").setIndicator("",getResources().getDrawable(R.drawable.transaction_refno))
              .setContent(intent);
tabHost.addTab(spec);

intent = new Intent().setClass(this, Tab4.class);
spec = tabHost.newTabSpec("Fourth").setIndicator("",getResources().getDrawable(R.drawable.transaction_refno))
              .setContent(intent);
tabHost.addTab(spec);

tabHost.getTabWidget().setLeftStripDrawable(color.black);
tabHost.getTabWidget().setRightStripDrawable(color.black);
tabHost.getTabWidget().setStripEnabled(false);

tabHost.getTabWidget().setCurrentTab(0);
tabHost.getTabWidget().setDividerDrawable(null);

所选标签显示蓝色下划线,我想更改其颜色。我该怎么做?

【问题讨论】:

  • 你试过drawable选择器
  • 你能给我们看看transaction_refnodrawables吗?

标签: android android-tabhost


【解决方案1】:

除了使用R.drawable.transaction_refno,您可以使用一个drawable,它使用选择器根据状态显示不同的drawable。你可以在你的drawable中写这个:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/tab_selected"
          android:state_selected="true"/>
    <item android:drawable="@drawable/tab_unselected"/>
</selector>

并为每个设计创建tab_selectedtab_unselected 可绘制对象。关于如何使用 TabHost 的更多信息,您可以查看Android docs 或查看this walkthrough

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-12-23
    • 2013-06-11
    • 1970-01-01
    • 1970-01-01
    • 2015-09-12
    • 2014-07-15
    • 1970-01-01
    相关资源
    最近更新 更多