我应该如何选择一个标签在TabLayout编程?

 TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
 tabLayout.setupWithViewPager(viewPager);

当前回答

用这个:

<android.support.design.widget.TabLayout
    android:id="@+id/patienthomescreen_tabs"
    android:layout_width="match_parent"
    android:layout_height="72sp"
    app:tabGravity="fill"
    app:tabMode="fixed"
    app:tabIndicatorColor="@android:color/white"
    app:tabSelectedTextColor="@color/green"/>

在OnClickListener中:

TabLayout tabLayout = (TabLayout) findViewById(R.id.patienthomescreen_tabs);
TabLayout.Tab tab = tabLayout.getTabAt(someIndex);
tab.select();

请记住,如果currentTabIndex和index是相同的,那么这将发送您的流到onTabReselected而不是onTabSelected。

其他回答

这将不工作的应用程序,有ViewPager2实现,为此,你需要使用

viewPager2.setCurrentItem(position);

当我们使用TabLayoutMediator时,在onConfigureTab中找到onConfigureTab

i.e

TabLayoutMediator tabLayoutMediator = new TabLayoutMediator(
                tabLayout, viewPager2, new TabLayoutMediator.TabConfigurationStrategy() {
            @Override
            public void onConfigureTab(@NonNull TabLayout.Tab tab, int position) {
                switch (position){
                    case 0 : tab.setIcon(getResources().getDrawable(R.drawable.camera));
                            break;
                    case 1 : tab.setText("CHAT");
                            viewPager2.setCurrentItem(position); // when app starts this will be the selected tab
                            break;
                    case 2 : tab.setText("STATUS");
                            break;
                    case 3 : tab.setText("CALL");
                        break;
                }
            }
        }
        );
        tabLayoutMediator.attach();

试试这个方法。

tabLayout.setTabTextColors(getResources().getColor(R.color.colorHintTextLight),
                           getResources().getColor(R.color.colorPrimaryTextLight));

如果你不能使用tab.select(),也不想使用ViewPager,你仍然可以通过编程方式选择一个选项卡。如果你通过TabLayout使用自定义视图。选项卡setCustomView (android.view。View View)它更简单。以下是如何做到这两种方式。

// if you've set a custom view
void updateTabSelection(int position) {
    // get the position of the currently selected tab and set selected to false
    mTabLayout.getTabAt(mTabLayout.getSelectedTabPosition()).getCustomView().setSelected(false);
    // set selected to true on the desired tab
    mTabLayout.getTabAt(position).getCustomView().setSelected(true);
    // move the selection indicator
    mTabLayout.setScrollPosition(position, 0, true);

    // ... your logic to swap out your fragments
}

如果你不使用自定义视图,那么你可以这样做

// if you are not using a custom view
void updateTabSelection(int position) {
    // get a reference to the tabs container view
    LinearLayout ll = (LinearLayout) mTabLayout.getChildAt(0);
    // get the child view at the position of the currently selected tab and set selected to false
    ll.getChildAt(mTabLayout.getSelectedTabPosition()).setSelected(false);
    // get the child view at the new selected position and set selected to true
    ll.getChildAt(position).setSelected(true);
    // move the selection indicator
    mTabLayout.setScrollPosition(position, 0, true);

    // ... your logic to swap out your fragments
}

使用StateListDrawable在选中和未选中的可绘制对象之间切换,或者类似地处理颜色和/或可绘制对象。

对于Viewpager2, Kotlin,其他答案都没有帮助,只有下面这个有用。位置是从片段结果监听器在我的情况下:

TabLayoutMediator(binding.tabLayout, binding.viewPager2) { _, _ ->
    binding.viewPager2 = position
}.attach()

对于材质组件库提供的TabLayout,只需使用selectTab方法:

TabLayout tabLayout = findViewById(R.id.tab_layout);
tabLayout.selectTab(tabLayout.getTabAt(index));

它需要版本为1.1.0。