【发布时间】:2018-02-15 21:13:01
【问题描述】:
我正在尝试使用自定义视图制作一些标签。这是我的代码
View tabContent = LayoutInflater.from(this).inflate(R.layout.tab_content, null);
TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
TextView tabText = (TextView) tabContent.findViewById(R.id.tabText);
tabText.setText("Tab 1");
tabLayout.addTab(tabLayout.newTab().setCustomView(tabContent));
tabText.setText("Tab 2");
tabLayout.addTab(tabLayout.newTab().setCustomView(tabContent));
tabText.setText("Tab 3");
tabLayout.addTab(tabLayout.newTab().setCustomView(tabContent));
但它只呈现给我第三个标签
现在,奇怪的是,如果我尝试只设置文本,如下所示:
tabLayout.addTab(tabLayout.newTab().setText("Tab 1"));
tabLayout.addTab(tabLayout.newTab().setText("Tab 2"));
tabLayout.addTab(tabLayout.newTab().setText("Tab 3"));
一切正常(文本被渲染)......但由于那个计数器,我需要那个自定义视图。
谁能解释一下为什么会这样?
【问题讨论】:
-
你设置了 1
TextView tabText3 次,最后一个是“Tab 3”,这可能就是它保持不变的原因......