Android 选项卡 - 设置自定义视图问题
Android tabs - set custom view issue
我正在尝试制作一些带有自定义视图的选项卡。这是我的代码
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"));
一切正常(文本已呈现)...但由于该计数器,我需要自定义视图。
谁能解释一下为什么会这样?
打开Activity.java并像下面的代码一样修改和设置标签
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 tab2 = (TextView) LayoutInflater.from(this).inflate(R.layout.custom_tab, null);
tab2.setText("TWO");
tab2.setCompoundDrawablesWithIntrinsicBounds(0, R.drawable.ic_tab_favourite, 0, 0);
tabLayout.getTabAt(1).setCustomView(tab2);
TextView tab3 = (TextView) LayoutInflater.from(this).inflate(R.layout.custom_tab, null);
tab3.setText("THREE");
tab3.setCompoundDrawablesWithIntrinsicBounds(0, R.drawable.ic_tab_favourite, 0, 0);
tabLayout.getTabAt(2).setCustomView(tab3);
试试这个:
View tabContent = LayoutInflater.from(this).inflate(R.layout.tab_content, null);
TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
TextView tabText1 = (TextView) tabContent.findViewById(R.id.tabText);
TextView tabText2 = (TextView) tabContent.findViewById(R.id.tabText);
TextView tabText3 = (TextView) tabContent.findViewById(R.id.tabText);
tabText1.setText("Tab 1");
tabLayout.getTabAt(0).setCustomView(tabText1);
tabText2.setText("Tab 2");
tabLayout.getTabAt(1).setCustomView(tabText2);
tabText3.setText("Tab 3");
tabLayout.getTabAt(2).setCustomView(tabText3);
需要将 tabContent
膨胀 3 次,因为 setCustomView()
方法直接与该实例一起使用,因此对 tabContent
对象的每次修改都会影响其余选项卡
我正在尝试制作一些带有自定义视图的选项卡。这是我的代码
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"));
一切正常(文本已呈现)...但由于该计数器,我需要自定义视图。
谁能解释一下为什么会这样?
打开Activity.java并像下面的代码一样修改和设置标签
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 tab2 = (TextView) LayoutInflater.from(this).inflate(R.layout.custom_tab, null);
tab2.setText("TWO");
tab2.setCompoundDrawablesWithIntrinsicBounds(0, R.drawable.ic_tab_favourite, 0, 0);
tabLayout.getTabAt(1).setCustomView(tab2);
TextView tab3 = (TextView) LayoutInflater.from(this).inflate(R.layout.custom_tab, null);
tab3.setText("THREE");
tab3.setCompoundDrawablesWithIntrinsicBounds(0, R.drawable.ic_tab_favourite, 0, 0);
tabLayout.getTabAt(2).setCustomView(tab3);
试试这个:
View tabContent = LayoutInflater.from(this).inflate(R.layout.tab_content, null);
TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
TextView tabText1 = (TextView) tabContent.findViewById(R.id.tabText);
TextView tabText2 = (TextView) tabContent.findViewById(R.id.tabText);
TextView tabText3 = (TextView) tabContent.findViewById(R.id.tabText);
tabText1.setText("Tab 1");
tabLayout.getTabAt(0).setCustomView(tabText1);
tabText2.setText("Tab 2");
tabLayout.getTabAt(1).setCustomView(tabText2);
tabText3.setText("Tab 3");
tabLayout.getTabAt(2).setCustomView(tabText3);
需要将 tabContent
膨胀 3 次,因为 setCustomView()
方法直接与该实例一起使用,因此对 tabContent
对象的每次修改都会影响其余选项卡