android更改标签指示器样式

时间:2014-02-17 10:54:51

标签: android android-styles

这是我的风格

<style name="MyCustomTheme" parent="@style/Theme.AppCompat">
        <item name="android:actionBarStyle">@style/MyActionBarTheme</item>
        <item name="actionBarTabTextStyle">@style/MyActionBarTabText</item>
        <item name="android:actionBarTabStyle">@style/customActionBarTabStyle</item>
        <item name="actionMenuTextColor">#fff</item>
    </style>
<style name="customActionBarTabStyle" parent="@style/Widget.AppCompat.ActionBar.TabView">

        <!-- tab indicator -->
        <item name="android:background">#ac0910</item>

        <!-- Support library compatibility -->
        <item name="background">@drawable/actionbar_tab_indicator</item>
    </style>

我得到了这个结果: enter image description here 但如果我将我的xml更改为: enter image description here

    <!-- tab indicator -->
    <item name="android:background">@drawable/actionbar_tab_indicator</item>

    <!-- Support library compatibility -->
    <item name="background">@drawable/actionbar_tab_indicator</item>
</style>

我得到了这个结果:

这是我的@ drawable / actionbar_tab_indicator

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

<!-- STATES WHEN BUTTON IS NOT PRESSED -->

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

    <!-- Focused states (such as when focused with a d-pad or mouse hover) -->
    <item android:state_focused="true" android:state_selected="false"
          android:state_pressed="false"
          android:drawable="@drawable/tab_unselected_focused" />
    <item android:state_focused="true" android:state_selected="true"
          android:state_pressed="false"
          android:drawable="@drawable/tab_selected_focused" />


<!-- STATES WHEN BUTTON IS PRESSED -->

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

    <!-- Focused states (such as when focused with a d-pad or mouse hover) -->
    <item android:state_focused="true" android:state_selected="false"
          android:state_pressed="true"
          android:drawable="@drawable/tab_unselected_pressed" />
    <item android:state_focused="true" android:state_selected="true"
          android:state_pressed="true"
          android:drawable="@drawable/tab_selected_pressed" />
</selector>

最重要的部分是

@绘制/ tab_selected_pressed @绘制/ tab_unselected_pressed @绘制/ tab_selected_pressed ...

是像rectange和填充颜色的图像。这就是我虽然是指标

1 个答案:

答案 0 :(得分:1)

在xml中创建自定义布局

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/tab_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/tab_button_drawable"
android:orientation="vertical"
android:padding="10dp" > <TextView
    android:id="@+id/tab_text_view"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textColor="@drawable/text_navigator_color_drawable"
    android:textSize="14dp" /></LinearLayout>

并创建一个方法,该方法将在定义选项卡视图后返回自定义视图,而不是定义视图

public View createTabView(final Context context, final String text) {
    View view = LayoutInflater.from(context).inflate(R.layout.tab_layout,
            null);
    TextView tv = (TextView) view.findViewById(R.id.tab_text_view);
    tv.setText(text);
    return view;
}

tabSpec.setIndicator(createTabView(tabHost.getContext(), tabWidgetTextView.getText().toString()));