addTab
在 API 21 中已弃用,因此我必须将选项卡布局更改为PagerTabStrip
,但我找不到将自定义 XML 布局添加到 pagerTabStrip 选项卡的方法...据我所知,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;
}
}
}