我TabLayout在我的应用中使用了Tabbed导航.我有一个非常奇怪的问题,我使用此代码创建了4个选项卡:
private int[] tabIcons = {R.drawable.navigation_timeline_icon_selector, R.drawable.navigation_feed_icon_selector,
R.drawable.navigation_messages_icon_selector, R.drawable.navigation_notification_icon_selector};
TabLayout tabLayout = setTabLayout();
if (tabLayout != null) {
for (int i = 0; i < 4; i++) {
tabLayout.getTabAt(i).setIcon(tabIcons[i]);
}
}
Run Code Online (Sandbox Code Playgroud)
tabIcon中的每个项目都是selector选定状态和非选定状态.所有图标选择器配置如下:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/navigation_timeline_selected_icon" android:state_selected="true"/>
<item android:drawable="@drawable/navigation_timeline_selected_icon" android:state_pressed="true"/>
<item android:drawable="@drawable/navigation_timeline_icon" />
</selector>
Run Code Online (Sandbox Code Playgroud)
问题是当应用程序启动时,第一个选定的选项卡(索引0)不使用选定的状态图标.相反,它使用非选择状态.
更详细的解释是这个问题的截图,首次启动我的选项卡如下所示:
相反,它应该是这样的:
更改页面后,所有图标都恢复为完整功能,并且正确选择了所选状态.
我尝试使用该TabLayout.Tab select()方法,但结果与使用的图标相同是未选择的图标.
有人知道我能做些什么来解决它吗?
我正在使用TabLayout设计库我想要实现的是
我已经尝试了很多教程,我可以通过自定义选项卡来实现它,但是当选择选项卡时会出现限制我想要更改文本颜色以及图标的图像,这是通过引用无法实现的到目前为止我读过的任何教程.到目前为止,我已经尝试过这个了FragmentStatePagerAdapter
public View getTabView(int position) {
View tab = LayoutInflater.from(mContext).inflate(R.layout.tabbar_view, null);
TextView tabText = (TextView) tab.findViewById(R.id.tabText);
ImageView tabImage = (ImageView) tab.findViewById(R.id.tabImage);
tabText.setText(mFragmentTitles.get(position));
tabImage.setBackgroundResource(mFragmentIcons.get(position));
if (position == 0) {
tab.setSelected(true);
}
return tab;
}
Run Code Online (Sandbox Code Playgroud)