简体   繁体   English

android更改标签指示器样式

[英]android change tab indicator style

This is my style 这是我的风格

<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>

I got this result: 我得到了这个结果: 在此处输入图片说明 but if i changed my xml to this: 但是如果我将xml更改为此: 在此处输入图片说明

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

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

I got this result: 我得到了这个结果:

This is my @drawable/actionbar_tab_indicator 这是我的@ 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>

the most important part is 最重要的部分是

@drawable/tab_selected_pressed @drawable/tab_unselected_pressed @drawable/tab_selected_pressed ... @ drawable / tab_selected_pressed @ drawable / tab_unselected_pressed @ drawable / tab_selected_pressed ...

are images like rectange and fill in colors. 是像矩形和填充颜色的图像。 this is what i though is the indicator 这就是我虽然是指标

create a custom layout in the xml as 在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>

and create a method which will return a custom view than define as this after defining the tab view 并创建一个方法,该方法将在定义选项卡视图后返回自定义视图,而不是按此定义

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()));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM