【问题标题】:Tabs in tablayout are not showing tab icontablayout 中的选项卡不显示选项卡图标
【发布时间】:2018-02-02 07:11:34
【问题描述】:

我已经创建了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);
        }
    });

所以现在它以黑色显示选项卡文本颜色。我希望标签文本颜色为白色并带有左侧图标。如何实现?

【问题讨论】:

  • 您已经在setupViewPager() 方法中为标签提供了标题。删除 setupTabIcons() 方法中的标题并尝试在 setupTabIcons() 中仅使用 tabLayout.getTabAt(0).setIcon(R.drawable.icon);
  • 删除这一行 tabLayout.post(new Runnable() { @Override public void run() { tabLayout.setupWithViewPager(viewPager); } });
  • @MohammedFarhan 感谢您的回复。但它给出了相同的输出
  • @Adilhusen。感谢您的回复。但现在它不显示任何标签的标题,只显示第一个标签的图标

标签: android android-fragments android-viewpager android-tablayout


【解决方案1】:

我为同样的问题苦苦挣扎了一段时间,直到我发现如果在设置图标后调用 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]);
    } 
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-06-26
    • 1970-01-01
    • 2016-10-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-08-28
    • 1970-01-01
    相关资源
    最近更新 更多