我制作了一个tablayout标签,其中包含标签的自定义布局,有时我希望更改这些标签的标题。但我没有正确的做法,请帮助我。
我的 custom_tab.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/titttle"
android:textStyle="bold"
android:text="MYTITLE"
android:paddingLeft="10dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/count"
android:textStyle="bold"
android:text="1"
android:paddingLeft="10dp"/>
</RelativeLayout>
并在 MainActivity.java
中tabLayout = (TabLayout) findViewById(R.id.tabs);
tabLayout.setupWithViewPager(viewPager);
tabLayout.getTabAt(0).setCustomView(R.layout.tab_custom_view);
tabLayout.getTabAt(1).setCustomView(R.layout.tab_custom_view);
TextView textView = (TextView)tabLayout.getChildAt(1).getRootView().findViewById(R.id.titttle);
textView.setText("New Title");
这里
TextView textView = (TextView)tabLayout.getChildAt(1).getRootView().findViewById(R.id.titttle);
textView.setText("New Title");
无效,请帮我更新自定义标签的textview。
答案 0 :(得分:3)
您需要先对视图进行充气。
View v = View.inflate(context, R.layout.tab_custom_view, null);
Text textView = (TextView) v.findViewById(R.id.titttle);
textView.setTitle("New Title");
tabLayout.getTabAt(0).setCustomView(v);