将viewbadger添加到Android L中的pagerTabStrip

时间:2015-01-20 06:58:37

标签: android xml layout badge pagertabstrip

在API 21中不推荐使用

addTab,因此我必须将我的标签布局更改为PagerTabStrip,但我找不到在pagerTabStrip标签中添加自定义XML布局的方法......据我了解,viewBadger需要扩充自定义XML。希望我的问题很快得到解答。

以下是我当前的TabPagerAdapter。

package com.example.gosh;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.text.SpannableStringBuilder;
import android.text.Spanned;
import android.text.style.ImageSpan;

public class TabPagerAdapterMain extends FragmentStatePagerAdapter {
    private String tabtitles[] = new String[] { "Home", "Task", "Shop","Wallet" };
Context mContext;
Drawable homeIcon;
Drawable taskIcon;
Drawable shopIcon;
Drawable walletIcon;

public TabPagerAdapterMain(FragmentManager fm, Context context) {
    super(fm);
    mContext = context;
}


@Override
public Fragment getItem(int i) {
    switch (i) {
        case 0:
            return new Home();
        case 1:
            return new Task();
        case 2:
            return new Shop();
        case 3:
            return new Wallet();
    }
    return null;
}

@Override
public int getCount() {
    // TODO Auto-generated method stub
    return 4; // No of Tabs
}

@Override
public CharSequence getPageTitle(int position) {
    homeIcon = mContext.getResources().getDrawable(R.drawable.home_active);
    taskIcon = mContext.getResources().getDrawable(R.drawable.task_active);
    shopIcon = mContext.getResources().getDrawable(R.drawable.shop_active);
    walletIcon = mContext.getResources().getDrawable(R.drawable.wallet_active);

    if(position == 0){
        SpannableStringBuilder sb = new SpannableStringBuilder("  "+tabtitles[position]); // space added before text for convenience

        homeIcon.setBounds(0, 0, homeIcon.getIntrinsicWidth(), homeIcon.getIntrinsicHeight());
        ImageSpan span = new ImageSpan(homeIcon, ImageSpan.ALIGN_BASELINE);
        sb.setSpan(span, 0, 1, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        return sb;
    }else if(position == 1){
        SpannableStringBuilder sb = new SpannableStringBuilder("  "+tabtitles[position]); // space added before text for convenience

        taskIcon.setBounds(0, 0, taskIcon.getIntrinsicWidth(), taskIcon.getIntrinsicHeight());
        ImageSpan span = new ImageSpan(taskIcon, ImageSpan.ALIGN_BASELINE);
        sb.setSpan(span, 0, 1, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        return sb;
    }else if(position == 2){
        SpannableStringBuilder sb = new SpannableStringBuilder("  "+tabtitles[position]); // space added before text for convenience

        shopIcon.setBounds(0, 0, shopIcon.getIntrinsicWidth(), shopIcon.getIntrinsicHeight());
        ImageSpan span = new ImageSpan(shopIcon, ImageSpan.ALIGN_BASELINE);
        sb.setSpan(span, 0, 1, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        return sb;
    }else if(position == 3){
        SpannableStringBuilder sb = new SpannableStringBuilder("  "+tabtitles[position]); // space added before text for convenience

        walletIcon.setBounds(0, 0, walletIcon.getIntrinsicWidth(), walletIcon.getIntrinsicHeight());
        ImageSpan span = new ImageSpan(walletIcon, ImageSpan.ALIGN_BASELINE);
        sb.setSpan(span, 0, 1, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        return sb;
    }else{
        return null;
    }

}
}

1 个答案:

答案 0 :(得分:0)

您始终可以在标签页中添加片段。我没有看到片段被导入到您的班级。确保您的片段已导入。