【问题标题】:Changing the icon of selected tab更改所选选项卡的图标
【发布时间】:2016-06-04 21:45:14
【问题描述】:

我将tablayout设置如下,

 private void setupTabIcons() {

        TextView tabOne = (TextView) LayoutInflater.from(this).inflate(R.layout.custom_tab, null);
        tabOne.setText("Status");
        tabOne.setCompoundDrawablesWithIntrinsicBounds(0, R.drawable.status, 0, 0);
        tabLayout.getTabAt(0).setCustomView(tabOne);

        TextView tabTwo = (TextView) LayoutInflater.from(this).inflate(R.layout.custom_tab, null);
        tabTwo.setText("Rating");
        tabTwo.setCompoundDrawablesWithIntrinsicBounds(0, R.drawable.rateit, 0, 0);
        tabLayout.getTabAt(1).setCustomView(tabTwo);
}

如何更改所选标签的图标?我正在使用 tablayout。

【问题讨论】:

    标签: android android-tablayout


    【解决方案1】:

    我正在使用TabLayout.OnTabSelectedListener

    wptabs.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                tab.setIcon(R.drawable.newicon);
                 //also you can use tab.setCustomView() too
            }
    
            @Override
            public void onTabUnselected(TabLayout.Tab tab) {
                tab.setIcon(R.drawable.oldicon);
            }
    
            @Override
            public void onTabReselected(TabLayout.Tab tab) {
    
            }
        });
    

    【讨论】:

    • 如何获取标签的索引。我总共有四个标签
    • tab.getPosition() 将返回标签位置。例如,如果你得到第一个标签位置=0 和第四个标签位置 =3
    猜你喜欢
    • 2018-02-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-12-20
    相关资源
    最近更新 更多