gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 19:58:06 25 4
gpt4 key购买 nike

我制作了一个 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 。

最佳答案

您需要先膨胀 View 。

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

关于android-fragments - Tablayout 更新自定义 View 选项卡的页面标题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31303335/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com