我有一个tablayout
,有5个标签。每个标签都有icon
和title
。我想更改所选标签的图标和标题的颜色以及 un-selected
标签。为此,我尝试使用自定义选项卡,但它不起作用。以下是 custom_tab.xml
:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<ImageView
android:layout_width="20dp"
android:layout_height="20dp"
android:layout_gravity="center"
android:id="@+id/tabIcon"/>
<com.prj.shopt.CustomTextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#C1C1C1"
android:gravity="center"
android:id="@+id/tabTitle"/>
</LinearLayout>
和Home2Activity
中的代码:
package com.prj.shopt;
import android.graphics.Typeface;
import android.support.design.widget.TabLayout;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.AppCompatTextView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
public class Home2Activity extends AppCompatActivity implements TabLayout.OnTabSelectedListener {
private TabLayout tabs;
private ViewPager viewPager;
private HomeTabsAdapter tabsAdapter;
private int[] activeIcons={R.drawable.homeactive,R.drawable.category_highlighted,R.drawable.heart_highlighted,
R.drawable.basketactive,R.drawable.avatar_highlighted};
private int[] deactiveIcons={R.drawable.homedeactive,R.drawable.category_default,R.drawable.heart_default,
R.drawable.basketdeactive,R.drawable.avatar_default};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home2);
tabs=findViewById(R.id.homeTabs);
viewPager=findViewById(R.id.tabsViewPager);
setupViewPager();
tabs.setupWithViewPager(viewPager);
setupTabIcons();
tabs.addOnTabSelectedListener(this);
}
private void setupTabIcons(){
if(tabs.getTabCount()<5)
return;
int len=activeIcons.length;
for(int j=0;j<len;j++) {
//tabs.getTabAt(j).setIcon(deactiveIcons[j]);
tabs.getTabAt(j).setCustomView(HomeTabsAdapter.getTabView(deactiveIcons[j], R.color.tab_inactive, "test", Home2Activity.this,tabs));
}
tabs.getTabAt(0).setCustomView(HomeTabsAdapter.getTabView(activeIcons[0],R.color.tab_active,"test",Home2Activity.this,tabs));
}
private void setupViewPager(){
tabsAdapter=new HomeTabsAdapter(getSupportFragmentManager());
viewPager.setAdapter(tabsAdapter);
setupFragments();
}
private void setupFragments() {
for(int i=0;i<5;i++)
tabsAdapter.addFragment(new FavFragment(),getResources().getString(R.string.favTab));
}
@Override
public void onTabSelected(TabLayout.Tab tab) {
//tab.setIcon(activeIcons[tab.getPosition()]);
tab.setCustomView(HomeTabsAdapter.getTabView(activeIcons[tab.getPosition()],R.color.tab_active,"test",Home2Activity.this,tabs));
}
@Override
public void onTabUnselected(TabLayout.Tab tab) {
//tab.setIcon(deactiveIcons[tab.getPosition()]);
tab.setCustomView(HomeTabsAdapter.getTabView(deactiveIcons[tab.getPosition()],R.color.tab_inactive,"test",Home2Activity.this,tabs));
}
@Override
public void onTabReselected(TabLayout.Tab tab) {
//tab.setIcon(activeIcons[tab.getPosition()]);
tab.setCustomView(HomeTabsAdapter.getTabView(activeIcons[tab.getPosition()],R.color.tab_active,"test",Home2Activity.this,tabs));
}
}
,这是我的适配器(HomeTabsAdapter.java
):
import android.content.Context;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import org.w3c.dom.Text;
import java.util.ArrayList;
import java.util.List;
public class HomeTabsAdapter extends FragmentPagerAdapter {
List<Fragment> fragments;
List<String> tabTitles;
public HomeTabsAdapter(FragmentManager fm) {
super(fm);
fragments=new ArrayList<>();
tabTitles=new ArrayList<>();
}
@Override
public Fragment getItem(int position) {
return fragments.get(position);
}
@Override
public int getCount() {
return fragments.size();
}
public void addFragment(Fragment fragment,String title){
fragments.add(fragment);
tabTitles.add(title);
notifyDataSetChanged();
}
public static View getTabView(int imageId, int color, String title, Context context, TabLayout tabLayout){
View v= LayoutInflater.from(context).inflate(R.layout.custom_tab,tabLayout,false);
TextView tabTitle=v.findViewById(R.id.tabTitle);
tabTitle.setText(title);
tabTitle.setTextColor(color);
tabTitle.setTypeface(Utility.getFontTypeFace(context));
ImageView icon=(v.findViewById(R.id.tabIcon));
icon.setImageResource(0);
icon.setImageResource(imageId);
return v;
}
@Override
public CharSequence getPageTitle(int position) {
return tabTitles.get(position);
}
}
结果是具有固定颜色的图标以及具有固定颜色的标题。选择和取消选择选项卡没有任何变化。 谢谢你的帮助
答案 0 :(得分:0)
使用此方法: -
private void setCustomTab(TabLayout tabLayout, int index, String title) {
TextView tab_home = (TextView) LayoutInflater.from(getActivity()).inflate(R.layout.custome_tab, null);
tab_home.setText(title);
tabLayout.getTabAt(index).setCustomView(tab_home);
}
使用以下方法: -
TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
setCustomTab(tabLayout, 0, "Upcoming");
setCustomTab(tabLayout, 1, "Completed");