根据应用程序的主题修改ViewPager选项卡

时间:2016-01-29 04:24:04

标签: android android-layout android-viewpager viewpagerindicator

我创建了一个简单的ViewPager tabview。 tabview有一个我设计的自定义主题。

tabview如下所示:

Tabview screenshot

tabview的布局XML文件

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.view.ViewPager
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/pager"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
</android.support.v4.view.ViewPager>

我为ViewPager创建的自定义主题:

<?xml version="1.0" encoding="utf-8"?>


<resources>

    <style name="Theme.Custom_tab_theme" parent="@android:style/Theme.Holo.Light">
        <item name="android:actionBarItemBackground">@drawable/selectable_background_custom_tab_theme</item>
        <item name="android:popupMenuStyle">@style/PopupMenu.Custom_tab_theme</item>
        <item name="android:dropDownListViewStyle">@style/DropDownListView.Custom_tab_theme</item>
        <item name="android:actionBarTabStyle">@style/ActionBarTabStyle.Custom_tab_theme</item>
        <item name="android:actionDropDownStyle">@style/DropDownNav.Custom_tab_theme</item>
        <item name="android:actionBarStyle">@style/ActionBar.Solid.Custom_tab_theme</item>
        <item name="android:actionModeBackground">@drawable/cab_background_top_custom_tab_theme</item>
        <item name="android:actionModeSplitBackground">@drawable/cab_background_bottom_custom_tab_theme</item>
        <item name="android:actionModeCloseButtonStyle">@style/ActionButton.CloseMode.Custom_tab_theme</item>
        <item name="actionMenuTextColor">@color/white</item>
        <item name="android:actionMenuTextColor">@color/white</item>

    </style>


    <style name="TextAppearance">
        <item name="android:textColor">@android:color/white</item>
    </style>

    <style name="ActionBar.Solid.Custom_tab_theme" parent="@android:style/Widget.Holo.Light.ActionBar.Solid">
        <item name="android:background">@drawable/ab_solid_custom_tab_theme</item>
        <item name="android:backgroundStacked">@drawable/ab_stacked_solid_custom_tab_theme</item>
        <item name="android:backgroundSplit">@drawable/ab_bottom_solid_custom_tab_theme</item>
        <item name="android:progressBarStyle">@style/ProgressBar.Custom_tab_theme</item>
    </style>

    <style name="ActionBar.Transparent.Custom_tab_theme" parent="@android:style/Widget.Holo.Light.ActionBar">
        <item name="android:background">@drawable/ab_transparent_custom_tab_theme</item>
        <item name="android:progressBarStyle">@style/ProgressBar.Custom_tab_theme</item>
    </style>

    <style name="PopupMenu.Custom_tab_theme" parent="@android:style/Widget.Holo.Light.ListPopupWindow">
        <item name="android:popupBackground">@drawable/menu_dropdown_panel_custom_tab_theme</item>  
    </style>

    <style name="DropDownListView.Custom_tab_theme" parent="@android:style/Widget.Holo.Light.ListView.DropDown">
        <item name="android:listSelector">@drawable/selectable_background_custom_tab_theme</item>
    </style>

    <style name="ActionBarTabStyle.Custom_tab_theme" parent="@android:style/Widget.Holo.Light.ActionBar.TabView">
        <item name="android:background">@drawable/tab_indicator_ab_custom_tab_theme</item>
    </style>

    <style name="DropDownNav.Custom_tab_theme" parent="@android:style/Widget.Holo.Light.Spinner">
        <item name="android:background">@drawable/spinner_background_ab_custom_tab_theme</item>
        <item name="android:popupBackground">@drawable/menu_dropdown_panel_custom_tab_theme</item>
        <item name="android:dropDownSelector">@drawable/selectable_background_custom_tab_theme</item>
    </style>

    <style name="ProgressBar.Custom_tab_theme" parent="@android:style/Widget.Holo.Light.ProgressBar.Horizontal">
        <item name="android:progressDrawable">@drawable/progress_horizontal_custom_tab_theme</item>
    </style>

    <style name="ActionButton.CloseMode.Custom_tab_theme" parent="@android:style/Widget.Holo.Light.ActionButton.CloseMode">
        <item name="android:background">@drawable/btn_cab_done_custom_tab_theme</item>
    </style>

    <!-- this style is only referenced in a Light.DarkActionBar based theme -->
    <style name="Theme.Custom_tab_theme.Widget" parent="@android:style/Theme.Holo">
        <item name="android:popupMenuStyle">@style/PopupMenu.Custom_tab_theme</item>
        <item name="android:dropDownListViewStyle">@style/DropDownListView.Custom_tab_theme</item>
    </style>

</resources>

现在我想自定义TabView以包含图标,并在选择时更改ViewPager的图标和文本颜色。

我想要实现的目标:

Output wanted

所以,基本上我想做三件事:

  1. 将图片添加到标签
  2. 选择时更改标签的图片和文字颜色
  3. 更改标签文字的字体样式(删除粗体并使用自定义字体)
  4. 我应该怎样做才能实现相同的目标?是否应修改自定义主题?

3 个答案:

答案 0 :(得分:0)

请从PagerAdapter尝试此操作。

private int[] imageResId = {
        R.drawable.ic_one,
        R.drawable.ic_two,
        R.drawable.ic_three
};

// ...

@Override
public CharSequence getPageTitle(int position) {
    // Generate title based on item position
    // return tabTitles[position];

    // getDrawable(int i) is deprecated, use getDrawable(int i, Theme theme) for min SDK >=21
    // or ContextCompat.getDrawable(Context context, int id) if you want support for older versions.
    // Drawable image = context.getResources().getDrawable(iconIds[position], context.getTheme());
    // Drawable image = context.getResources().getDrawable(imageResId[position]);

    Drawable image = ContextCompat.getDrawable(context, imageResId[position]);
    image.setBounds(0, 0, image.getIntrinsicWidth(), image.getIntrinsicHeight());
    SpannableString sb = new SpannableString(" ");
    ImageSpan imageSpan = new ImageSpan(image, ImageSpan.ALIGN_BOTTOM);
    sb.setSpan(imageSpan, 0, 1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
    return sb;
}

答案 1 :(得分:0)

试试这个

1)将图片添加到标签页

    // here you add an array of your icon   
 final int[] ICONS = new int[] {
                    R.drawable.ic_browse,
                    R.drawable.ic_start,
                    R.drawable.ic_collage,
            };

for (int i=0; i < tabs.length; i++)
        {
        actionBar.addTab(actionBar.newTab().setText(tabs[i])
                                 .setIcon(NewsFeedActivity.this.getResources().getDrawable(ICONS[i]))
                                 .setTabListener(this));
        }//endfor

2)选择时更改标签的图片和文字颜色

您可以使用活动的tabHost.getCurrentTab() onResume()来检测您选择的标签。

答案 2 :(得分:0)

我假设你正在使用设计库中的TabLayout(如果你不是,你可能应该这样,因为它很容易使用)。课程文档解释了如何使用ViewPager进行设置,但这只会为标签提供标题而不是图标。您可以随后自行添加图标:

TabLayout tabLayout = (TabLayout) findViewById(...);
// normal setup, text-only tabs
tabLayout.setupWithViewPager(viewPager);
// add the icons
for (int i = 0; i < tabLayout.getTabCount(); i++) {
    TabLayout.Tab tab = tabLayout.getTabAt(i);
    tab.setIcon(R.drawable.tab_icon); // different icon per tab
}

要为图标着色,您可以使用state list drawablesandroid:state_selected="true"显示不同颜色的图标。您可以通过创建color state list和相同的状态属性来为文本执行相同的操作。

要使文本不是粗体,您可以将自己的文本外观应用于TabLayout。通过制作样式声明来做到这一点(注意我也在这里应用上述颜色状态列表):

<!-- TextAppearance.Design.Tab is the default used in the Design Library -->
<style name="TabTextAppearance" parent="TextAppearance.Design.Tab">
    <item name="android:textColor">@color/tab_text_color_state_list</color>
    <item name="android:fontFamily">sans-serif</color>
</style>

然后在你的布局中使用它:

<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    ... >

    <android.support.design.widget.TabLayout
        android:id="@+id/pager"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:minHeight="?android:attr/actionBarSize"
        app:tabTextAppearance="@style/TabTextAppearance" />

    <android.support.v4.view.ViewPager
        android:id="@+id/pager"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1" />

</LinearLayout>