【问题标题】:Use different Color for different ActionBar.Tab对不同的 ActionBar.Tab 使用不同的颜色
【发布时间】:2013-08-11 21:27:56
【问题描述】:

我希望 ActionBar 中的所有选项卡都有不同的颜色指示器,例如选项卡 1 为蓝色,选项卡 2 为红色等。

为了实现这一点,我确实为所有颜色创建了不同的选择器,并将它们放在可绘制的不同 xml 中。在 style.xml 中,我通过

调用它们
<style name="MyTheme" parent="AppBaseTheme">
<item name="android:actionBarTabStyle">@style/ActionBarTabStyleRed</item>
</style>
<style name="ActionBarTabStyleRed">
<item name="android:background">@drawable/tab_indicator_red</item>
</style>

我也为不同的颜色创建了这种样式。现在,当我将可绘制对象或样式更改为不同的颜色时,它可以工作了。我可以看到颜色被应用于选项卡。但由于所有标签都是相同的颜色,它并没有解决我的目的。我试图在onTabSelected() 中设置标签样式,但没有办法做到这一点。

最终我尝试为不同的颜色创建不同的主题并尝试在onTabSelected() 中以编程方式设置它们,但后来我知道必须在setContentView() 之前设置主题。
所以我的问题是..我该怎么做?有什么办法可以为不同的标签指示器设置不同的颜色???

更新:-
drawable/tab_indicator_red.xml

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <!-- Non focused states -->
    <item android:state_focused="false" android:state_selected="false" android:state_pressed="false" android:drawable="@android:color/transparent" />
    <item android:state_focused="false" android:state_selected="true"  android:state_pressed="false" android:drawable="@drawable/tab_selected_red" />

    <!-- Focused states -->
    <item android:state_focused="true" android:state_selected="false" android:state_pressed="false" android:drawable="@drawable/tab_unselected_focused_red" />
    <item android:state_focused="true" android:state_selected="true"  android:state_pressed="false" android:drawable="@drawable/tab_selected_focused_red" />

    <!-- Pressed -->
    <!--    Non focused states -->
    <item android:state_focused="false" android:state_selected="false" android:state_pressed="true" android:drawable="@drawable/tab_unselected_pressed_red" />
    <item android:state_focused="false" android:state_selected="true"  android:state_pressed="true" android:drawable="@drawable/tab_selected_pressed_red" />

    <!--    Focused states -->
    <item android:state_focused="true" android:state_selected="false" android:state_pressed="true" android:drawable="@drawable/tab_unselected_pressed_red" />
    <item android:state_focused="true" android:state_selected="true"  android:state_pressed="true" android:drawable="@drawable/tab_selected_pressed_red" />
</selector>

【问题讨论】:

  • 我有一个,但我必须清除一些东西-> 在选择器中,你有什么代替“...”?您是否对不同的颜色使用不同的样式?你如何使用样式。如果所有选项卡都必须是不同的颜色,你如何分别设置它们(或者这就是这里的问题)?
  • @g00dy 更新了问题的更多细节。你没看错,这就是这里的问题。我对所有可能导致不同选项卡的颜色指示符不同的方式持开放态度,无论是通过 xml、选择器还是通过 java 代码。我更喜欢最不麻烦的方式。 :-)

标签: android tabs android-actionbar android-3.0-honeycomb


【解决方案1】:

我想我对你有一个起点,但它有点繁琐。 使用以下代码设置操作栏,您可以设置选项卡以使用您的自定义外观:

  //...somewhere in oncreate
  ActionBar bar = getActionBar();

    bar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

    ActionBar.Tab tab1 = bar.newTab();
    TextView tv1 = createTab("Tab 1", R.drawable.firsttabdrawable);
    tab1.setCustomView(tv1);
    tab1.setTabListener(this);
    ActionBar.Tab tab2 = bar.newTab();
    tab2.setCustomView(createTab("Tab 2", R.drawable.secondTabDrawable));
    tab2.setTabListener(this);
    ActionBar.Tab tab3 = bar.newTab();
    tab3.setCustomView(createTab("Tab 3", R.drawable.thirdTabDrawable));
    tab3.setTabListener(this);
    ActionBar.Tab tab4 = bar.newTab();
    tab4.setCustomView(createTab("Tab 4", R.drawable.fourthTabDrawable));
    tab4.setTabListener(this);  

    bar.addTab(tab1);
    bar.addTab(tab2);
    bar.addTab(tab3);
    bar.addTab(tab4);

createTab 方法如下:

  private TextView createTab(String titleText, int tabBackgroundDrawableId)
{
    TextView tv = new TextView(this);
    //set caption and caption appearance
    tv.setTextAppearance(this, R.style.MyDefaultTabTextAppearance);
    tv.setText(titleText);
    //set appearance of tab
    tv.setBackgroundResource(tabBackgroundDrawableId);
    tv.setLayoutParams(new android.view.ViewGroup.LayoutParams(android.view.ViewGroup.LayoutParams.MATCH_PARENT, android.view.ViewGroup.LayoutParams.WRAP_CONTENT));
    //Make sure all tabs have the same height
    tv.setMaxLines(2);
    tv.setMinLines(2);

    return tv;      
}

但是,操作栏似乎将选项卡视图添加到添加填充的父视图中,因此您需要将其删除。我在第一个代码块之后的 oncreate 中这样做了:

  View view = (View) tv1.getParent();       
    LinearLayout.LayoutParams lp = (android.widget.LinearLayout.LayoutParams)view.getLayoutParams();
    view.setPadding(0, 0, 0, 0);
    view.setLayoutParams(lp);

您可能会找到一种更优雅/直接的方式来执行此操作,但作为起点,我认为值得发布。

希望这会有所帮助。

添加:

还可能值得指出的是,如果您的选项卡布局更复杂,您可以在 createTab 方法中扩充布局 xml 文件。例如使用以下 xml (mytab.xml):

  <?xml version="1.0" encoding="utf-8"?>
  <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:layout_width="match_parent"
   android:layout_height="wrap_content">
<ImageView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/tabIcon"/>
<TextView style="@style/MyDefaultTabTextAppearance"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_toRightOf="@+id/tabIcon"
    android:minLines="2"
    android:maxLines="2"
    android:id="@+id/tabText"/>
  </RelativeLayout>

createTab 变成:

  private RelativeLayout createTab(String titleText, int tabBackgroundDrawableId)
{
    RelativeLayout rl = (RelativeLayout)this.getLayoutInflater().inflate(R.layout.mytab, null, false);  
     //set appearance of tab
    rl.setBackgroundResource(tabBackgroundDrawableId);

    TextView tv = (TextView)rl.findViewById(R.id.tabText);
    //set caption       
    tv.setText(titleText);     

    ImageView iv = (ImageView)rl.findViewById(R.id.tabIcon);

    iv.setImageResource(R.drawable.ic_launcher);//or supply unique drawable to method?      
    return rl;      
}

【讨论】:

  • 这是一个很好的解决方案。我在想使用自定义视图是这里唯一的解决方案。我没有使用 textview,而是使用 RelativeLayout 作为自定义视图。问题是即使在将父填充设置为 0 之后,我在 RelativeLayout 中设置的图标也不在中心,并且在横向模式下,选项卡会被拧紧。让我知道你是否也有任何解决方法。虽然问题几乎解决了,因为我可以设置相关布局的背景,但它在现有指标上覆盖了各种。所以可能我也需要隐藏现有的指标。
  • PS:我的标签只有图标和指示器,没有文本视图
  • @Creator 我在我的答案中添加了一些内容,可以帮助您使用相对布局作为选项卡。我已经测试了从纵向到横向的示例,并且看不到选项卡布局有任何问题,但是如果您可以进一步详细说明,我可以尝试提供帮助。如果您的标签背景没有完全覆盖您的标签可用的整个方形空间,我建议添加一个背景为纯色的外部布局(线性/相对),例如黑色。
  • 谢谢您,我已经根据您的回答实施了一个解决方案,忘记将其标记为已接受。对此感到抱歉。再次感谢您的补充。干杯!!!
猜你喜欢
  • 1970-01-01
  • 2023-03-25
  • 1970-01-01
  • 2012-08-24
  • 1970-01-01
  • 2017-08-08
  • 1970-01-01
  • 1970-01-01
  • 2017-04-20
相关资源
最近更新 更多