我创建了tablayout。并在每个标签的左侧添加了图标。但我无法看到这些图标。 以下是java文件的代码
public class ActivityAllVerification extends AppCompatActivity {
SessionManager sessionManager;
Toolbar toolbar;
private TabLayout tabLayout;
private ViewPager viewPager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_all_verification);
initDefaultSettings();
}
private void initDefaultSettings() {
sessionManager = new SessionManager(ActivityAllVerification.this);
toolbar = (Toolbar) findViewById(R.id.appbar);
setSupportActionBar(toolbar);
((ImageView) toolbar.findViewById(R.id.bitcoin_logo)).setVisibility(View.GONE);
TextView txt = (TextView) toolbar.findViewById(R.id.toolbar_txt_title);
txt.setText("Account Verification");
getSupportActionBar().setDisplayShowHomeEnabled(true);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setDisplayShowTitleEnabled(false);//disable default action bar title
// Toast.makeText(this, "" + sessionManager.isUser(), Toast.LENGTH_SHORT).show();
viewPager = (ViewPager) findViewById(R.id.viewpager);
setupViewPager(viewPager);
tabLayout = (TabLayout) findViewById(R.id.tabs);
tabLayout.setupWithViewPager(viewPager);
setupTabIcons();
tabLayout.post(new Runnable() {
@Override
public void run() {
tabLayout.setupWithViewPager(viewPager);
}
});
}
private void setupTabIcons() {
TextView tabOne = (TextView) LayoutInflater.from(this).inflate(R.layout.custom_tab, null);
tabOne.setText("PAN CARD");
tabOne.setCompoundDrawablesWithIntrinsicBounds(R.drawable.ic_bitcoin_24, 0, 0, 0);
tabLayout.getTabAt(0).setCustomView(tabOne);
TextView tabTwo = (TextView) LayoutInflater.from(this).inflate(R.layout.custom_tab, null);
tabTwo.setText("AADHAR CARD");
tabOne.setCompoundDrawablesWithIntrinsicBounds(R.drawable.ic_bitcoin_24, 0, 0, 0);
tabLayout.getTabAt(1).setCustomView(tabTwo);
TextView tabThree = (TextView) LayoutInflater.from(this).inflate(R.layout.custom_tab, null);
tabThree.setText("BACK ACCOUNT");
tabOne.setCompoundDrawablesWithIntrinsicBounds(R.drawable.ic_bitcoin_24, 0, 0, 0);
tabLayout.getTabAt(2).setCustomView(tabThree);
Toast.makeText(this, "" + sessionManager.isUser(), Toast.LENGTH_SHORT).show();
if (!sessionManager.isUser())
{
TextView tabFour = (TextView) LayoutInflater.from(this).inflate(R.layout.custom_tab, null);
tabThree.setText("TAX INCOICE");
tabOne.setCompoundDrawablesWithIntrinsicBounds(R.drawable.ic_bitcoin_24, 0, 0, 0);
tabLayout.getTabAt(3).setCustomView(tabFour);
}
}
private void setupViewPager(ViewPager viewPager) {
ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
adapter.addFragment(new FragmentPanCard(), "PAN CARD");
adapter.addFragment(new FragmentAadharCard(), "AADAHR CARD");
adapter.addFragment(new FragmentBankAccount(), "BANK ACCOUNT");
if (!sessionManager.isUser())
adapter.addFragment(new FragmentTaxInvoice(), "TAX INVOICE");
viewPager.setAdapter(adapter);
}
class ViewPagerAdapter extends FragmentPagerAdapter {
private final List<Fragment> mFragmentList = new ArrayList<>();
private final List<String> mFragmentTitleList = new ArrayList<>();
public ViewPagerAdapter(FragmentManager manager) {
super(manager);
}
@Override
public Fragment getItem(int position) {
return mFragmentList.get(position);
}
@Override
public int getCount() {
return mFragmentList.size();
}
public void addFragment(Fragment fragment, String title) {
mFragmentList.add(fragment);
mFragmentTitleList.add(title);
}
@Override
public CharSequence getPageTitle(int position) {
return mFragmentTitleList.get(position);
}
}
}
这里来自here我创建了自定义标签,在setupTabIcons()中,我在标签左侧设置了图标。这是customtab.xml的代码
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="4dp"
android:id="@+id/tab"
android:textColor="@color/colorWhite"
android:fontFamily="@string/font_fontFamily_medium"/>
这里是activity_all_verification.xml的代码
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/background_color">
<include
android:id="@+id/appbar"
layout="@layout/toolbar"></include>
<android.support.design.widget.TabLayout
android:id="@+id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/appbar"
app:tabGravity="fill"
app:tabMode="scrollable"
app:tabIndicatorColor="@color/colorWhite"
app:tabTextColor="@color/colorWhite"/>
<android.support.v4.view.ViewPager
android:id="@+id/viewpager"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@+id/tabs"
app:layout_behavior="@string/appbar_scrolling_view_behavior" />
</RelativeLayout>
第一次标签显示不正确所以从enter link description here我在.java文件中添加了以下代码
tabLayout.post(new Runnable() {
@Override
public void run() {
tabLayout.setupWithViewPager(viewPager);
}
});
所以现在它显示标签文字颜色为黑色。我想要标签文字颜色为白色和左侧图标。如何实现它?
答案 0 :(得分:0)
一段时间以来,我一直在同一个问题上苦苦挣扎,直到我发现如果在设置完图标后调用SectionsPageAdapter的 notifyDataSetChanged ,则不会显示标签图标。
只需确保每次调用notifyDataSetChanged()后都设置图标
private SectionsPagerAdapter mSectionsPagerAdapter;
private ViewPager mViewPager;
TabLayout tabLayout;
private int[] tabIcons = {
R.drawable.icon_tab_settings,
R.drawable.icon_tab_edit,
R.drawable.icon_tab_recordings,
R.drawable.icon_tab_pentagram
};
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
...
mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
mViewPager = findViewById(R.id.container);
mViewPager.setAdapter(mSectionsPagerAdapter);
mViewPager.setCurrentItem(1);
tabLayout = findViewById(R.id.tabs);
tabLayout.setupWithViewPager(mViewPager);
tabLayout.getTabAt(0).setIcon(tabIcons[0]);
tabLayout.getTabAt(1).setIcon(tabIcons[1]);
tabLayout.getTabAt(2).setIcon(tabIcons[2]);
tabLayout.getTabAt(3).setIcon(tabIcons[3]);
...
}
public void refreshView(){
if(mSectionsPagerAdapter!=null){
mSectionsPagerAdapter.notifyDataSetChanged();
//refresh tab icons again
tabLayout.getTabAt(0).setIcon(tabIcons[0]);
tabLayout.getTabAt(1).setIcon(tabIcons[1]);
tabLayout.getTabAt(2).setIcon(tabIcons[2]);
tabLayout.getTabAt(3).setIcon(tabIcons[3]);
}
}