[英]Android tab background doesnt change on unselected
I need to change the background of a tab once selected set the unselected tab backgroud color back to the old one. 选中后,我需要更改标签的背景,将未选中的标签背景颜色设置回旧的。 For some reason the background changes only if I click on another tab after IE: I press on the first tab - correct color.
出于某种原因,仅当我在IE之后单击另一个选项卡时,背景才会更改:我按第一个选项卡-正确的颜色。 Press on the second tab - the first one keeps the highlighted color and the second tab is also highlighted.
按第二个标签-第一个标签保持突出显示的颜色,第二个标签也突出显示。 Press on the 3rd one - the first one is back to normal, the second one is still highlighted and the 3rd one is also highlighted.
按第三个按钮-第一个按钮恢复正常,第二个仍然突出显示,第三个也突出显示。
Here is my TabLayout and one of my tabs (They are all the same design) 这是我的TabLayout和我的一个标签(它们都是相同的设计)
<android.support.design.widget.TabLayout
android:id="@+id/tab_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#dbdbdb"
app:tabPaddingBottom="-1dp"
app:tabPaddingEnd="-1dp"
app:tabPaddingStart="-1dp"
app:tabPaddingTop="-1dp"
app:tabBackground="@drawable/tabs_background_colors"
android:layout_alignParentBottom="true"
android:layout_alignParentStart="true" >
android:background="@android:color/darker_gray" >
<android.support.design.widget.TabItem
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:icon="@drawable/reservation_w"
android:id="@+id/tab_reservation"
android:layout="@layout/tab_item_with_icon"
android:layout_weight="1" />
The tab_item_with_icon tab_item_with_icon
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_vertical"
android:background="#00000000">
<ImageView android:id="@android:id/icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="5dp"
android:scaleType="fitCenter"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true" />
and my tab listener 和我的标签听众
((TabLayout)findViewById(R.id.tab_layout)).addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
@Override
public void onTabSelected(TabLayout.Tab tab) {
viewPager.setCurrentItem(tab.getPosition());
int icon = 0;
switch (tab.getPosition())
{
case 0:
icon = R.drawable.reservation_w;
break;
case 1:
icon = R.drawable.offers_w;
break;
case 2:
icon = R.drawable.folio_w;
break;
case 3:
icon = R.drawable.location_w;
break;
case 4:
icon = R.drawable.contact_w;
break;
}
tab.setIcon(icon);
}
@Override
public void onTabUnselected(TabLayout.Tab tab) {
int icon = 0;
switch (tab.getPosition())
{
case 0:
icon = R.drawable.reservation_g;
break;
case 1:
icon = R.drawable.offers_g;
break;
case 2:
icon = R.drawable.folio_g;
break;
case 3:
icon = R.drawable.location_g;
break;
case 4:
icon = R.drawable.contact_g;
break;
}
tab.setIcon(icon);
In addition this is my states xml for the tab 另外,这是我的标签状态XML
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@color/tab_background_selected" android:state_selected="true"/>
<item android:drawable="@color/tab_background_unselected" android:state_selected="false"/>
And here it how it looks in case I wasnt that clear with my demonstration 如果我的示范没有弄清楚的话, 这里的样子
Thanks in advance :) 提前致谢 :)
Apparently it looks like a bug... but setting the icon in the onTabUnselect would make it so that the background wouldnt change... not sure why and would be glad if someone can explain it. 显然,它看起来像是个错误……但是在onTabUnselect中设置图标会使它变成背景不会改变……不知道为什么,并且如果有人可以解释它会很高兴。
Anyway if you got into this mess the solution is to not change the icons in onTabUnselect but only on the onTabSelect event. 无论如何,如果您陷入困境,解决方案是不更改onTabUnselect中的图标,而仅更改onTabSelect事件。 What I did was save the position of the unselected tab into a global int from the onTabUnselect
我所做的是从onTabUnselect将未选择的选项卡的位置保存到全局int
@Override
public void onTabUnselected(TabLayout.Tab tab) {
unselectedTabPosition = tab.getPosition();
}
and then change the icons on the onTabSelect - 然后更改onTabSelect上的图标-
@Override
public void onTabSelected(TabLayout.Tab tab) {
changeToGrayIcon(unselectedTabPosition);
Hope this will save the time I wasted on this for someone. 希望这可以节省我为某人浪费的时间。
Your first xml contains errror. 您的第一个xml包含错误。 Check android:background property.
检查android:background属性。 Run the method tab.setIcon(icon) on UI thread.
在UI线程上运行方法tab.setIcon(icon)。
YourActivity.this.runOnUiThread(new Runnable() {
public void run() {
tab.setIcon(icon);
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.