标签指示灯颜色

时间:2013-09-12 09:36:31

标签: android

enter image description here如何在不使用图像的情况下设置选项卡小部件的tabindicator颜色。

private TextView makeTabIndicator(String text, Context context) {
    int tabHeight = 44;
    //String tab_text_color = context.getString(R.string.fixed_tab_text_color);
    TextView tabView = new TextView(getContext());
    tabView.setBackgroundColor(Utils.getColor("#0a223a"));
    LayoutParams lp3 = new LayoutParams(LayoutParams.WRAP_CONTENT, CommonUtils.getDimension(tabHeight), 1);
    //lp3.setMargins(1, 0, 1, 0);
    tabView.setLayoutParams(lp3);
    tabView.setText(text);
    //tabView.setTextColor(Utils.getColor(tab_text_color));
    tabView.setGravity(Gravity.CENTER_HORIZONTAL | Gravity.CENTER_VERTICAL);

    ColorDrawable unselectedState = new ColorDrawable(Utils.getColor("2c8efd"));
    ColorDrawable selectedState = new ColorDrawable(Utils.getColor("00ffff"));
    ColorDrawable focusState = new ColorDrawable(Utils.getColor("ffffff"));
    ColorDrawable pressedState = new ColorDrawable(Utils.getColor(""));

    StateListDrawable sld = new StateListDrawable();

    sld.addState(new int[] { android.R.attr.state_selected }, selectedState);
    sld.addState(new int[] { android.R.attr.state_pressed }, pressedState);
    sld.addState(new int[] { android.R.attr.state_focused }, focusState);
    sld.addState(new int[] {}, unselectedState);

    tabView.setBackgroundDrawable(sld);
    tabView.setPadding(2, 0, 2, 0);
    return tabView;
}

这是为整个标签设置颜色。但是,我想仅为标签文本下面的行提供颜色,高度为5dp。请告诉我们如何实现这一目标。

5 个答案:

答案 0 :(得分:7)

您在所选标签底部看到的蓝线实际上是9-patch drawable。一组6个不同的9-patch drawable用于创建状态选择器drawable。此状态选择器drawable用作与TabSpec.setIndicator(View)一起使用的视图的背景。

以下状态组合在默认状态选择器drawable中解决:

<?xml version="1.0" encoding="utf-8"?>
<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="@drawable/tab_unselected_holo" />
<item android:state_focused="false" android:state_selected="true"  android:state_pressed="false" android:drawable="@drawable/tab_selected_holo" />

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

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

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

因此,如果要创建StateListDrawable,则应该解决所有这些状态组合。此外,ColorDrawables将使用指定的颜色填充整个选项卡。如果你只需要更改底部的蓝线,你需要自己创建9-patch drawables。

现在,转到:

  

[你硬盘上的android sdk安装目录]&gt; [sdk]&gt; [平台]&gt; [android-XX(XX&gt; 11)]&gt; [数据]&gt; [res]&gt; [drawable-hdpi]

从上面的状态选择器中找到相应的drawable(例如,您将要查找的第一个drawable是tab_unselected_holo.9.png)。用图像编辑器打开它们(http://pixlr.com/editor/就可以了)并用您选择的颜色更改实心蓝色部分。将这些drawable保存在项目的res/drawable-hdpi文件夹中。使用.9.png扩展名来命名它们时要小心。从上面的代码创建一个状态选择器,并将drawables更改为您创建的那些。将此状态选择器保存在项目的res/drawable中。对于以下代码,我假设您将此状态选择器命名为my_tab_drawable.xml

创建名为tab_indicator.xml的xml布局文件。我们将使用它来创建选项卡的视图:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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/my_tab_drawable"   <<==== state-selector drawable
    android:gravity="center"
    android:orientation="vertical"
    android:padding="10dip" >

    <TextView
        android:id="@+id/tabsText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="15dip" />

</LinearLayout>

将您的makeTabIndicator(String, Context)更改为:

private View makeTabIndicator(String text, Context context) {

    // Inflate the layout file we defined above
    View view = LayoutInflater.from(context).inflate(R.layout.tab_indicator, null);   

    TextView tv = (TextView) view.findViewById(R.id.tabsText);
    tv.setText(text);

    return view;

}

请注意,我们正在返回View代替TextView。您将makeTabIndicator(String, Context)用作:

mtTabSpec.setIndicator(makeTabIndicator("TAB TEXT", this));

或者,如果您想动态创建TextView(正如您当前所做的那样),则无需创建my_tab_drawable.xml或定义布局tab_indicator.xml

private TextView makeTabIndicator(String text, Context context) {
    int tabHeight = 44;
    //String tab_text_color = context.getString(R.string.fixed_tab_text_color);
    TextView tabView = new TextView(getContext());
    //tabView.setBackgroundColor(Utils.getColor("#0a223a"));
    LayoutParams lp3 = new LayoutParams(LayoutParams.WRAP_CONTENT, CommonUtils.getDimension(tabHeight), 1);
    //lp3.setMargins(1, 0, 1, 0);
    tabView.setLayoutParams(lp3);
    tabView.setText(text);
    //tabView.setTextColor(Utils.getColor(tab_text_color));
    tabView.setGravity(Gravity.CENTER_HORIZONTAL | Gravity.CENTER_VERTICAL);

    StateListDrawable sld = new StateListDrawable();

    sld.addState(new int[] { android.R.attr.state_selected }, context.getResources().getDrawable(R.drawable.tab_selected_holo_changed));

    sld.addState(new int[] { android.R.attr.state_focused }, context.getResources().getDrawable(R.drawable.tab_unselected_focused_holo_changed));

    sld.addState(new int[] { android.R.attr.state_focused, android.R.attr.state_selected }, 
            context.getResources().getDrawable(R.drawable.tab_selected_focused_holo_changed));

    sld.addState(new int[] { android.R.attr.state_pressed },
            context.getResources().getDrawable(R.drawable.tab_unselected_pressed_holo_changed));

    sld.addState(new int[] { android.R.attr.state_selected, android.R.attr.state_pressed },
            context.getResources().getDrawable(R.drawable.tab_selected_pressed_holo_changed));

    sld.addState(new int[] { android.R.attr.state_focused, android.R.attr.state_pressed },
            context.getResources().getDrawable(R.drawable.tab_unselected_pressed_holo_changed));

    sld.addState(new int[] { android.R.attr.state_focused, android.R.attr.state_pressed, android.R.attr.state_selected },
            context.getResources().getDrawable(R.drawable.tab_selected_pressed_holo_changed));

    tabView.setBackgroundDrawable(sld);

    // Consider increasing the padding values
    tabView.setPadding(2, 0, 2, 0);
    return tabView;
}

您仍然需要创建9-patch drawable。

如果您需要帮助准备/更换9-patch drawables,请告诉我。

答案 1 :(得分:3)

将此library用于自定义视图寻呼机并更改以下drawable的颜色。

  1. tab_selected_holo.9.png
  2. tab_unselected_holo.9.png

答案 2 :(得分:0)

使用不同事件的RGB颜色在您的drawable中创建自定义选择器,例如

<!--

<item android:state_selected="false" android:state_focused="false"
    android:state_pressed="false" android:drawable="@drawable/tab_unselected" />
-->

<item android:drawable="@drawable/tripple_tab_selected" android:state_focused="false" android:state_pressed="false" android:state_selected="true"/>
<!-- Inactive tab -->
<item android:drawable="@drawable/tripple_tab_unselected" android:state_focused="false" android:state_pressed="false" android:state_selected="false"/>

<!-- Pressed tab -->
<item android:drawable="@android:color/transparent" android:state_pressed="true"/>
<!-- Selected tab (using d-pad) -->
<item android:drawable="@android:color/transparent" android:state_focused="true" android:state_pressed="false" android:state_selected="true"/>

答案 3 :(得分:0)

您可以使用图层draawable来实现效果。例如,对于其中一个州, 使用名称header_background_layer_drawable.xml创建一个xml文件,如下所示。

<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >

    <item android:drawable="@color/aw_wsblue"/>
    <item
        android:bottom="5dp"
        android:drawable="@color/black"/>

</layer-list> 

将其作为背景添加到您的tabview,如下所示。

tabView.setBackgroundDrawable(getResources()getDrawable(R.drawable.header_background_layer_drawable));

答案 4 :(得分:-1)

    private View makeTabIndicator(Drawable drawable){
    ImageView Tabimage = new ImageView(this);
    LayoutParams LP = new           LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT,1);
    LP.setMargins(1, 0, 1, 0);
    Tabimage.setLayoutParams(LP);
    Tabimage.setImageDrawable(drawable);
    Tabimage.setBackgroundResource(R.drawable.tabview);
    return Tabimage;
 }

Google中的Google change the tab color android甚至是stackoverflow。这个问题已经得到了尽可能多的回答。

所有最佳。