使用数组简化代码

你好,世界

由于此代码是重复的,因此我决定对其进行更改

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

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

TextView tabThree = (TextView) LayoutInflater.from(this).inflate(R.layout.custom_tab, null);
tabThree.setText("THREE");
tabThree.setCompoundDrawablesWithIntrinsicBounds(0, R.drawable.ic_tab_contacts, 0, 0);
tabLayout.getTabAt(2).setCustomView(tabThree);

进入 :

private void SetupTab() {
    TextView[] Tab = new TextView[3];
    int[] tabIcons = {
        R.drawable.tab1,
        R.drawable.tab2,
        R.drawable.tab3
    };
    String[] tabTitle = {"ONE","TWO","THREE"};

    for (int i = 0; i < 3; i++) {
        TextView Tab[i] = (TextView) LayoutInflater.from(this).inflate(R.layout.custom_tab, null);
        Tab[i].setText(tabTitle[i]);
        Tab[i].setCompoundDrawablesWithIntrinsicBounds(0, tabIcons[i], 0, 0);
        tabLayout.getTabAt(i).setCustomView(Tab[i]);
    }
}

但是我遇到了我的程序无法正常工作的问题。有人可以帮我解决吗?非常感谢您的帮助。

恩村

我认为这TextView[] Tab是不必要的:

private void SetupTab() {
    int[] tabIcons = {
            R.drawable.tab1,
            R.drawable.tab2,
            R.drawable.tab3
    };
    String[] tabTitle = {"ONE","TWO","THREE"};

    for (int i = 0; i < 3; i++) {
        TextView view = (TextView) LayoutInflater.from(this).inflate(R.layout.custom_tab, null);
        view.setText(tabTitle[i]);
        view.setCompoundDrawablesWithIntrinsicBounds(0, tabIcons[i], 0, 0);
        tabLayout.getTabAt(i).setCustomView(view);
    }
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

PHP如何使用数组来简化代码

来自分类Dev

使用语法糖简化数组diff代码

来自分类Dev

如何使用LINQ简化代码

来自分类Dev

使用循环功能简化代码

来自分类Dev

使用for循环来简化代码

来自分类Dev

简化“切换”数组项的代码

来自分类Dev

简化此数组插入代码的方法?

来自分类Dev

简化使用数组填充变量

来自分类Dev

使用Ramda简化数组的计算

来自分类Dev

简化使用数组填充变量

来自分类Dev

有什么办法可以使用数组或其他方法在此处简化代码吗?

来自分类Dev

有什么办法可以使用数组或其他方法在此处简化代码吗?

来自分类Dev

如何创建数组并映射到数组以简化代码?

来自分类Dev

如何使用循环简化我的代码

来自分类Dev

JavaScript。尝试简化代码,也许使用循环?

来自分类Dev

使用DO循环简化重复的代码

来自分类Dev

在python中使用反字典简化代码

来自分类Dev

如何使用循环简化此代码

来自分类Dev

如何使用循环简化我的代码

来自分类Dev

如何使用for循环来简化代码?

来自分类Dev

如何使用 IF 条件简化 C# 代码

来自分类Dev

在尝试用html canvas和javascript绘制塞舌尔标志后,尝试使用数组或循环来简化我的代码

来自分类Dev

子集化时避免使用R简化数组

来自分类Dev

简化代码:二维数组的高度和宽度

来自分类Dev

如何使用Java 8功能简化重试代码块

来自分类Dev

如何使用嵌套协程和Promise简化JS代码?

来自分类Dev

如何使用它来改进和简化代码

来自分类Dev

如何使用CSV文件简化Python中的登录代码错误?

来自分类Dev

使用当前资源简化Devise控制器中的代码