在选中和取消选中状态下更改 Tablayout 中的图标
Change icon in Tablayout on selected and deselected state
抱歉,如果之前有人问过这个问题。
我想在选项卡布局的选项卡中选中时更改图标。我怎样才能使用选择器做到这一点?
我的应用程序中有两个选项卡的选定状态图标应该更改。
请检查这些问题,您将得到答案
Change icon and title color when selected in android design library TabLayout
tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
@Override
public void onTabSelected(TabLayout.Tab tab) {
tab.setIcon(selectedImageResources[tab.getPosition()]);
getSupportActionBar().setTitle(pageTitles[tab.getPosition()]);
viewPager.setCurrentItem(tab.getPosition());
}
@Override
public void onTabUnselected(TabLayout.Tab tab) {
tab.setIcon(imageResources[tab.getPosition()]);
}
@Override
public void onTabReselected(TabLayout.Tab tab) {
}
});
要制作选项卡选择器和取消选择器,您可以使用这种方式
1.Create 自定义视图并对其进行膨胀:
private View getTabView(int imgDrawable) {
View view = getLayoutInflater().inflate(R.layout.tab_view, null);
ImageView imgTab = (ImageView) view.findViewById(R.id.imgTab);
imgTab.setImageDrawable(getResources().getDrawable(imgDrawable));
return view;
}
2.Create 可绘制选择器
tab_home_selector.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/ic_home_selected" android:state_selected="true" />
<item android:drawable="@drawable/ic_home_deselected" />
</selector>
3.Insert 在选项卡中:
tabDashboardLayout = (TabLayout) findViewById(R.id.tabDashboardLayout);
//Adding the tabs using addTab() method
View tabView = getTabView(R.drawable.tab_home_selector);;
tabDashboardLayout.addTab(tabDashboardLayout.newTab().setCustomView(tabView));
对于单独的选项卡,您可以创建单独的可绘制选择器并添加到选项卡
请试试这个
tabLayout.getTabAt(0).setIcon(R.drawable.selector);
1.创建自定义选项卡选择器- 您需要添加状态选择 true 和 false
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/home_fill"
android:state_selected="true"/>
<item android:drawable="@drawable/home_line"
android:state_selected="false"/>
</selector>
2。添加自定义选项卡选择器可绘制作为 tabItem
的图标
<android.support.design.widget.TabLayout
android:id="@+id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/footer"
app:tabIndicatorColor="@color/female_colour">
<android.support.design.widget.TabItem
android:id="@+id/tab_home"
android:layout_width="@dimen/_25dp"
android:layout_height="@dimen/_25dp"
android:padding="@dimen/_4dp"
android:icon="@drawable/tab_home"
/>
</android.support.design.widget.TabLayout>
代码已在 Android 版本 7.1.1
上测试
抱歉,如果之前有人问过这个问题。
我想在选项卡布局的选项卡中选中时更改图标。我怎样才能使用选择器做到这一点?
我的应用程序中有两个选项卡的选定状态图标应该更改。
请检查这些问题,您将得到答案
Change icon and title color when selected in android design library TabLayout
tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
@Override
public void onTabSelected(TabLayout.Tab tab) {
tab.setIcon(selectedImageResources[tab.getPosition()]);
getSupportActionBar().setTitle(pageTitles[tab.getPosition()]);
viewPager.setCurrentItem(tab.getPosition());
}
@Override
public void onTabUnselected(TabLayout.Tab tab) {
tab.setIcon(imageResources[tab.getPosition()]);
}
@Override
public void onTabReselected(TabLayout.Tab tab) {
}
});
要制作选项卡选择器和取消选择器,您可以使用这种方式
1.Create 自定义视图并对其进行膨胀:
private View getTabView(int imgDrawable) {
View view = getLayoutInflater().inflate(R.layout.tab_view, null);
ImageView imgTab = (ImageView) view.findViewById(R.id.imgTab);
imgTab.setImageDrawable(getResources().getDrawable(imgDrawable));
return view;
}
2.Create 可绘制选择器
tab_home_selector.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/ic_home_selected" android:state_selected="true" />
<item android:drawable="@drawable/ic_home_deselected" />
</selector>
3.Insert 在选项卡中:
tabDashboardLayout = (TabLayout) findViewById(R.id.tabDashboardLayout);
//Adding the tabs using addTab() method
View tabView = getTabView(R.drawable.tab_home_selector);;
tabDashboardLayout.addTab(tabDashboardLayout.newTab().setCustomView(tabView));
对于单独的选项卡,您可以创建单独的可绘制选择器并添加到选项卡
请试试这个
tabLayout.getTabAt(0).setIcon(R.drawable.selector);
1.创建自定义选项卡选择器- 您需要添加状态选择 true 和 false
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/home_fill"
android:state_selected="true"/>
<item android:drawable="@drawable/home_line"
android:state_selected="false"/>
</selector>
2。添加自定义选项卡选择器可绘制作为 tabItem
的图标<android.support.design.widget.TabLayout
android:id="@+id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/footer"
app:tabIndicatorColor="@color/female_colour">
<android.support.design.widget.TabItem
android:id="@+id/tab_home"
android:layout_width="@dimen/_25dp"
android:layout_height="@dimen/_25dp"
android:padding="@dimen/_4dp"
android:icon="@drawable/tab_home"
/>
</android.support.design.widget.TabLayout>
代码已在 Android 版本 7.1.1
上测试