TabLayout图标未显示



我试图使tab与tablayout以下https://guides.codepath.com/android/Google-Play-Style-Tabs-using-TabLayout#add-custom-view-to-tablayout

现在我尝试使用imagespan显示图标而不是文本。但如果没有运气,谁能帮我指出本教程缺少什么?

下面是代码

public class HomeActivity extends FragmentActivity {
//Fragments List
public ArrayList <Fragment> fragmentList;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_home);
    setUpFragmentList();
    // Get the ViewPager and set it's PagerAdapter so that it can display items
    ViewPager viewPager = (ViewPager) findViewById(R.id.home_viewpager);
    viewPager.setAdapter(new HomeFragmentPagerAdapter(getSupportFragmentManager(), HomeActivity.this, fragmentList));
    // Give the TabLayout the ViewPager
    TabLayout tabLayout = (TabLayout) findViewById(R.id.sliding_tabs);
    //start this 2 are to set the tab to fill entire screen
    tabLayout.setTabMode(TabLayout.MODE_FIXED);
    tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);
    //end this 2 are to set the tab to fill entire screen
    tabLayout.setupWithViewPager(viewPager);
    tabLayout.getTabAt(1).setIcon(R.drawable.ic_tab_down_activity);
}
private void setUpFragmentList() {
    fragmentList = new ArrayList<>();
    fragmentList.add(new MyActivitesFragment());
    fragmentList.add(new ChatListFragment());
    fragmentList.add(new QuickMatchFragment());
    fragmentList.add(new FilterMatchFragment());
}
}

public class HomeFragmentPagerAdapter extends FragmentPagerAdapter {
private String tabTitles[] = new String[] { "Tab1", "Tab2", "Tab3" ,"Tab4"};
private Context context;
private ArrayList <Fragment> fragmentList;
private int[] imageResId = {
        // unclicked
        R.drawable.ic_tab_down_activity,
        R.drawable.ic_tab_down_chat,
        R.drawable.ic_tab_down_find,
        R.drawable.ic_tab_down_filter
};
public HomeFragmentPagerAdapter(FragmentManager fm, Context context, ArrayList <Fragment> fragmentList) {
    super(fm);
    this.context = context;
    this.fragmentList = fragmentList;
}
@Override
public int getCount() {
    return fragmentList.size();
}
@Override
public Fragment getItem(int position) {
    return fragmentList.get(position);
}
@Override
public CharSequence getPageTitle(int position) {
    Drawable image = context.getResources().getDrawable(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;
}
}

在您的自定义标签布局文件中,只需将textAllCaps设置为false。它会工作的:)原因是,图像正在被转换为字符,如果它为真,那么将它们转换为大写将不会渲染图像。

<style name="MyCustomTabTextAppearance" parent="TextAppearance.Design.Tab">
    <item name="android:textSize">16sp</item>
    <item name="android:textColor">?android:textColorSecondary</item>
    <item name="textAllCaps">false</item>
</style>

显然我已经找到了解决方案,使用TabLayout.getTabAt(i).setIcon(imageResId[i]);

首先我的意图是能够设置所有的图标从viewpager适配器。但由于我既不知道也没时间找到解决办法。我将使用我发现的另一个技巧。

和切换图像周围我使用TabLayout。TabLayoutOnPageChangeListener

希望这也能帮助到需要帮助的人。div =]

好吧,我猜你错过了你的xml的要点。设置标签布局的文本外观样式。即textAllCaps = false,否则图像span将无法工作

您还没有添加任何选项卡。

像这样添加一个标签:tabLayout.addTab (tabLayout.newTab()。setText("选项卡"));。

相关内容

  • 没有找到相关文章

最新更新