Vra*_*gle 5 android-fragments android-activity android-tablelayout android-tabs android-design-library
我制作了一个 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>
Run Code Online (Sandbox Code Playgroud)
并在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");
Run Code Online (Sandbox Code Playgroud)
这里
TextView textView = (TextView)tabLayout.getChildAt(1).getRootView().findViewById(R.id.titttle);
textView.setText("New Title");
Run Code Online (Sandbox Code Playgroud)
不工作,帮我更新自定义选项卡的文本视图。
您需要先膨胀视图。
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);
Run Code Online (Sandbox Code Playgroud)