【问题标题】:Can't arrange TabLayout icons to the left无法将 TabLayout 图标排列到左侧
【发布时间】:2018-11-12 17:13:15
【问题描述】:

在我的项目中我有这个代码

tabLayout.getTabAt(0).setIcon(R.drawable.location);
tabLayout.getTabAt(1).setIcon(R.drawable.list);

这使得图标出现在文本的顶部,我需要它们在左边

我试过了:

ConstraintLayout cl = (ConstraintLayout) LayoutInflater.from(this).inflate(R.layout.tab, null);
TextView tab = cl.findViewById(R.id.textViewTab);
tab.setText("Ver mapa");
tab.setTextColor(getResources().getColor(R.color.tabActive));
tab.setCompoundDrawablesRelativeWithIntrinsicBounds(R.drawable.location, 0, 0, 0);
tabLayout.getTabAt(0).setCustomView(tab);

tab.setText("Ver lista");
tab.setTextColor(getResources().getColor(R.color.tabInactive));
tab.setCompoundDrawablesRelativeWithIntrinsicBounds(R.drawable.list, 0, 0, 0);
tabLayout.getTabAt(1).setCustomView(tab);

但我得到了这个结果

漂亮,但不是我想要的

【问题讨论】:

  • 为什么你不通过 xml 这样做
  • 这会有什么不同吗?

标签: android android-tablayout


【解决方案1】:

我忘了初始化第二个视图:

ConstraintLayout cl = (ConstraintLayout) LayoutInflater.from(this).inflate(R.layout.tab, null);
TextView tab = cl.findViewById(R.id.textViewTab);
tab.setText("Ver mapa");
tab.setTextColor(getResources().getColor(R.color.tabActive));
tab.setCompoundDrawablesRelativeWithIntrinsicBounds(R.drawable.location, 0, 0, 0);
tabLayout.getTabAt(0).setCustomView(tab);

tab = cl.findViewById(R.id.textViewTab); // This was missing     tab.setText("Ver lista");
tab.setTextColor(getResources().getColor(R.color.tabInactive));
tab.setCompoundDrawablesRelativeWithIntrinsicBounds(R.drawable.list, 0, 0, 0);
tabLayout.getTabAt(1).setCustomView(tab);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-02-18
    • 1970-01-01
    • 2019-12-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多