我正在使用tablayout和框架布局。
此外,当我选择一个特定的标签时,图标应该将颜色更改为红色(我使用的是png图标,它们是黑色的,可以将它们更改为红色以表示它已被选中)
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.design.widget.TabLayout
android:id="@+id/simple_tab_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:tabBackground="@color/colorWhite"
app:tabIndicatorColor="#f00"
app:tabSelectedTextColor="#f00"
app:tabTextColor="#000"
/>
<FrameLayout
android:id="@+id/fl_home"
android:layout_width="match_parent"
android:layout_height="match_parent">
</FrameLayout>
</LinearLayout>
这是我的MainActivity
pb.setIcon(R.drawable.p_icon); // pb is my TabLayout.Tab
mb.setIcon(R.drawable.view_p_icon);
gb.setIcon(R.drawable.c_icon);
ptTab.setIcon(R.drawable.c_icon);
tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
@Override
public void onTabSelected(TabLayout.Tab tab)
{
switch (tab.getPosition()) {
case 0:
getSupportFragmentManager().beginTransaction().replace(R.id.fl_home, new UF()).commit();
break;
case 1:
//getSupportFragmentManager().beginTransaction().replace(R.id.fl_home, new DF()).commit();
showAlertDialog("Logout?");
break;
case 2:
//getSupportFragmentManager().beginTransaction().replace(R.id.fl_home, new SF()).commit();
showAlertDialog("Logout?");
break;
case 3:
//getSupportFragmentManager().beginTransaction().replace(R.id.fl_home, new SF()).commit();
showAlertDialog("Logout?");
break;
case 4:
getSupportFragmentManager().beginTransaction().replace(R.id.fl_home, new SF()).commit();
break;
}
}
@Override
public void onTabUnselected(TabLayout.Tab tab) {
}
@Override
public void onTabReselected(TabLayout.Tab tab) {
}
});
}
答案 0 :(得分:0)
在setColorFilter
方法中添加onTabSelected()
方法的代码。它会改变图像的颜色。
tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
@Override
public void onTabSelected(TabLayout.Tab tab) {
int tabPosition = tab.getPosition();
if (tabPosition == 0) {
// active tabName
imageOne.setColorFilter(getResources().getColor(R.color.redColor));
// other deactive tabName
imageTwo.setColorFilter(getResources().getColor(R.color.blackcolor));
imageThree.setColorFilter(getResources().getColor(R.color.blackcolor));
imageFour.setColorFilter(getResources().getColor(R.color.blackcolor));
} else if (tabPosition == 1) {
// active tabName
imageTwo.setColorFilter(getResources().getColor(R.color.redColor));
// other deactive tabName
imageOne.setColorFilter(getResources().getColor(R.color.blackcolor));
imageThree.setColorFilter(getResources().getColor(R.color.blackcolor));
imageFour.setColorFilter(getResources().getColor(R.color.blackcolor));
} else if (tabPosition == 2) {
// active tabName
imageThree.setColorFilter(getResources().getColor(R.color.redColor));
// other deactive tabName
imageTwo.setColorFilter(getResources().getColor(R.color.blackcolor));
imageOne.setColorFilter(getResources().getColor(R.color.blackcolor));
imageFour.setColorFilter(getResources().getColor(R.color.blackcolor));
} else if (tabPosition == 3) {
// active tabName
imageFour.setColorFilter(getResources().getColor(R.color.redColor));
// other deactive tabName
imageTwo.setColorFilter(getResources().getColor(R.color.blackcolor));
imageThree.setColorFilter(getResources().getColor(R.color.blackcolor));
imageOne.setColorFilter(getResources().getColor(R.color.blackcolor));
}
}
@Override
public void onTabUnselected(TabLayout.Tab tab) {
int tabPosition = tab.getPosition();
}
@Override
public void onTabReselected(TabLayout.Tab tab) {
int tabPosition = tab.getPosition();
}
});
选择标签作为默认选项。
如果与viewPager一起使用,
tabLayout = (TabLayout) findViewById(R.id.tablayout);
tabLayout.setupWithViewPager(viewpager);
viewpager.setCurrentItem(0); // set any number of tab which you want to select by default.
如果没有使用viewPager,那么,
tabLayout = (TabLayout) findViewById(R.id.tablayout);
TabLayout.Tab tab = tabLayout.getTabAt(someIndex); // which you want to select.
tab.select();
希望它有所帮助。