我正在开发使用选项卡主机的应用程序。我想使用5个片段,但只想在Tab主机上显示4个。
我要显示这样的标签托管
但是当我使用5个标签时,它会像这样显示。
这是我的viewPager代码:
private void setupViewPager(ViewPager viewPager) {
ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
adapter.addFragment(new Friends(),getResources().getString(R.string.friends));
adapter.addFragment(new Messages(),getResources().getString(R.string.messages));
adapter.addFragment(new History(),getResources().getString(R.string.history));
adapter.addFragment(new Discover(),getResources().getString(R.string.discover));
adapter.addFragment(new Connect(),"");
viewPager.setAdapter(adapter);
}
我正在使用Tabhost指示器显示图像和文本:
private int[] navIcons = {
R.drawable.ic_person,
R.drawable.ic_message,
R.drawable.ic_history,
R.drawable.ic_discover
};
private int[] navLabels = {
R.string.friends,
R.string.messages,
R.string.history,
R.string.discover
};
并实现它们:
for (int i = 0; i < 4; i++) {
LinearLayout tab = (LinearLayout) LayoutInflater.from(this).inflate(R.layout.nav_tab, null);
TextView tab_label = (TextView) tab.findViewById(R.id.nav_label);
ImageView tab_icon = (ImageView) tab.findViewById(R.id.nav_icon);
tab_label.setText(getResources().getString(navLabels[i]));
tabLayout.getTabAt(i).setCustomView(tab);
}