我有一个tablayout有4个选项卡。当我选择一个选项卡时,该选项卡的文本和图标应该是红色的,其他选项卡应该是灰色的。Android tabLayout最后选择的标签颜色没有正确更新
要更新图标,我创建了两个不同的版本,并在重写的OnTabSelectedListener中更新它,但是当我将其添加到Tablayout时,文本颜色似乎“落后”。最后选中的标签文本仍然是红色,并且在我按下另一个新标签之前不会更新。
当我删除OnTabSelectedListener,颜色正常工作,但我不能更新图标...我不能直接更新tabLayout.tab项目上的文本颜色。
这是Android中的错误还是我错过了什么?
activity_tab.xml:
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/main_content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context="com.example.oivind.tabsexample.TabActivity">
<android.support.design.widget.AppBarLayout
android:id="@+id/appbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom">
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="@android:color/darker_gray"/>
<android.support.design.widget.TabLayout
android:id="@+id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:tabIndicatorHeight="0dp"
app:tabTextColor="@android:color/darker_gray"
app:tabSelectedTextColor="@android:color/holo_red_light"
android:background="@android:color/white"
style="@style/NASTabLayout">
</android.support.design.widget.TabLayout>
</android.support.design.widget.AppBarLayout>
<android.support.v4.view.ViewPager
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent" />
tabActivity.java的onCreate:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_tab);
mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
viewPager = (ViewPager) findViewById(R.id.container);
viewPager.setAdapter(mSectionsPagerAdapter);
TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
tabLayout.setupWithViewPager(viewPager);
tabLayout.getTabAt(0).setIcon(R.drawable.ic_icon_error);
for(int i = 1; i < tabLayout.getTabCount(); i++) {
tabLayout.getTabAt(i).setIcon(R.drawable.ic_icon_error_grey);
}
tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
@Override
public void onTabSelected(TabLayout.Tab tab) {
tab.setIcon(R.drawable.ic_icon_error);
}
@Override
public void onTabUnselected(TabLayout.Tab tab) {
tab.setIcon(R.drawable.ic_icon_error_grey);
}
@Override
public void onTabReselected(TabLayout.Tab tab) {
tab.setIcon(R.drawable.ic_icon_error_grey);
}
});
}
这只发生在我将其设置回“OnTabUnselected”中的灰色图标时。真的好像是Android中的一个错误。 – Otziii