android fragments - Tablayout更新自定义视图选项卡的页面标题



我做了一个tablayout标签,它有标签的自定义布局,有时我想改变这些标签的标题。但是我不知道怎么做,请帮助我。

我的<<p> strong> 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

您需要首先对视图进行充气。

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);

最新更新