选中后在TabLayout中更改选项卡上的图像 [英] Change image on Tab in TabLayout when Selected
本文介绍了选中后在TabLayout中更改选项卡上的图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在使用设计TabLayout,
I am using Design TabLayout,
<android.support.design.widget.TabLayout
android:id="@+id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:tabBackground="@color/ColorPrimary"
app:tabIndicatorColor="@color/orange"
app:tabIndicatorHeight="3dp"
/>
我已将customview添加到选项卡
I have added customview to Tabs
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@null">
<ImageView
android:id="@+id/imgTab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/tab_image"
android:layout_centerHorizontal="true"
android:focusable="true"
android:focusableInTouchMode="true"/>
<TextView
android:id="@+id/tabTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/imgTab"
android:text="Home"
android:background="@null"
android:layout_centerHorizontal="true"
android:textColor="@color/selector_tab_text"/>
</RelativeLayout>
tab.setCustomView(view);
tab.setCustomView(view);
我想当标签被选中以改变自定义视图图像. 在imageview上使用选择器尝试过,它不起作用. 我无法在运行时中将视图分配给Tab,它仅包含setCustomView方法. 如何实现?
I want to change image in the custom view when Tab is Selected. Tried using Selector on imageview it does not work. I cannot get view assigned to Tab in runtime, it only contains setCustomView methode. How to achieve it?
推荐答案
将setOnTabSelectedListener
添加到tabLayout
对象
我命名为我的
navigation
navigation.setOnTabSelectedListener(
new TabLayout.ViewPagerOnTabSelectedListener(mainView) {
@Override
public void onTabSelected(TabLayout.Tab tab) {
super.onTabSelected(tab);
// 1. get the custom View you've added
View tabView = tab.getCustomView();
// get inflated children Views the icon and the label by their id
TextView tab_label = (TextView) tabView.findViewById(R.id.nav_label);
ImageView tab_icon = (ImageView) tabView.findViewById(R.id.nav_icon);
// change the label color, by getting the color resource value
tab_label.setTextColor(getResources().getColor(R.color.active_color));
// change the image Resource
// i defined all icons in an array ordered in order of tabs appearances
// call tab.getPosition() to get active tab index.
tab_icon.setImageResource(navIconsActive[tab.getPosition()]);
}
// do as the above the opposite way to reset tab when state is changed
// as it not the active one any more
@Override
public void onTabUnselected(TabLayout.Tab tab) {
super.onTabUnselected(tab);
View tabView = tab.getCustomView();
TextView tab_label = (TextView) tabView.findViewById(R.id.nav_label);
ImageView tab_icon = (ImageView) tabView.findViewById(R.id.nav_icon);
// back to the black color
tab_label.setTextColor(getResources().getColor(R.color.dark_grey));
// and the icon resouce to the old black image
// also via array that holds the icon resources in order
// and get the one of this tab's position
tab_icon.setImageResource(navIcons[tab.getPosition()]);
}
@Override
public void onTabReselected(TabLayout.Tab tab) {
super.onTabReselected(tab);
}
}
);
这将完美地完成这项工作,您可以像我自己做的那样将自己垫在后面:D
that will do the job perfectly , and you can pad yourself in the back as i did myself :D
这篇关于选中后在TabLayout中更改选项卡上的图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文