无法将 TabLayout 图标排列到左侧
Can't arrange TabLayout icons to the left
在我的项目中我有这段代码
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);
但我得到了这个结果
漂亮,但不是我想要的
我忘了初始化第二个视图:
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);
在我的项目中我有这段代码
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);
但我得到了这个结果
漂亮,但不是我想要的
我忘了初始化第二个视图:
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);