Android 表格布局图标未显示

Android 表格布局图标未显示,android,icons,imagespan,Android,Icons,Imagespan,我正试着在下面用tablayout制表 现在我尝试使用imagespan显示图标而不是文本。但是如果运气不好,有人能指出本教程缺少什么吗 这是密码 public class HomeActivity extends FragmentActivity { //Fragments List public ArrayList <Fragment> fragmentList; @Override protected void onCreate(Bundle savedInstanceSt

我正试着在下面用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;
}
}
公共类HomeActivity扩展了FragmentActivity{
//片段列表
公共阵列列表碎片列表;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
setUpFragmentList();
//获取ViewPager并将其设置为PagerAdapter,以便它可以显示项目
ViewPager ViewPager=(ViewPager)findViewById(R.id.home\u ViewPager);
setAdapter(新的HomeFragmentPagerAdapter(getSupportFragmentManager(),HomeActivity.this,fragmentList));
//将视图页导航提供给表格布局
TabLayout TabLayout=(TabLayout)findViewById(R.id.SLIVING_选项卡);
//启动此2个按钮,将选项卡设置为填充整个屏幕
tabLayout.setTabMode(tabLayout.MODE_固定);
tabLayout.setTabGravity(tabLayout.GRAVITY\u-FILL);
//结束此操作2是设置选项卡以填充整个屏幕
tabLayout.setupWithViewPager(viewPager);
tabLayout.getTabAt(1).setIcon(R.drawable.ic\u tab\u down\u活动);
}
私有void setUpFragmentList(){
fragmentList=新的ArrayList();
添加(新的MyActivitesFragment());
添加(新的ChatListFragment());
添加(新的QuickMatchFragment());
添加(新的FilterMatchFragment());
}
}
公共类HomeFragmentPagerAdapter扩展了FragmentPagerAdapter{
私有字符串tabTitles[]=新字符串[]{“Tab1”、“Tab2”、“Tab3”、“Tab4”};
私人语境;
私有ArrayList碎片列表;
私有int[]imageResId={
//解开
R.drawable.ic\U tab\U down\U活动,
R.drawable.ic\u选项卡\u down\u聊天,
R.drawable.ic\u tab\u down\u find,
R.drawable.ic\U选项卡\U下拉\U过滤器
};
公共HomeFragmentPagerAdapter(FragmentManager格式、上下文上下文、ArrayList fragmentList){
超级(fm);
this.context=上下文;
this.fragmentList=碎片列表;
}
@凌驾
public int getCount(){
返回fragmentList.size();
}
@凌驾
公共片段getItem(int位置){
返回碎片列表。获取(位置);
}
@凌驾
公共字符序列getPageTitle(int位置){
Drawable image=context.getResources().getDrawable(imageResId[position]);
setBounds(0,0,image.getIntrinsicWidth(),image.getIntrinsicHeight());
SpannableString sb=新SpannableString(“”);
ImageSpan ImageSpan=新的ImageSpan(图像,ImageSpan.ALIGN_-BOTTOM);
sb.setSpan(图像SPAN,0,1,SPANABLE.SPAN_EXCLUSIVE_EXCLUSIVE);
归还某人;
}
}

您还没有添加任何选项卡

添加如下选项卡:
tabLayout.addTab(tabLayout.newTab().setText(“Tab One”)

我想您缺少了xml中的要点。为选项卡布局设置文本外观样式。i、 e textAllCaps=false,否则图像范围将无法工作。

在自定义选项卡布局文件中,只需将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>

16便士
?android:textColorSecondary
假的

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

首先,我的目的是能够从viewpager适配器设置所有图标。但是因为我不知道也没有时间去寻找解决方案。相反,我会使用我发现的另一个技巧

另外,为了切换图像,我使用TabLayout.TabLayoutOnPageChangeListener


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

这部分看起来不错,显示所有标签代码。谢谢你的帮助,它解决了我的问题!在styles.xml中添加了MyCustomTabTextAppearance,并在activity xml下的android.support.design.widget.TabLayout视图上设置app:tabTextAppearance=“@style/MyCustomTabTextAppearance”属性。