【问题标题】:Android desing support lib change the Tab indicatorAndroid 设计支持库更改 Tab 指示器
【发布时间】:2026-01-05 01:00:01
【问题描述】:

目前无法从设计支持库中设置 TabLayout 的 tabindicator 中的 drawable。 还有其他方法可以将 drwable 设置为 tabindicator 吗?

【问题讨论】:

  • tabSpec.setIndicator(view); ??

标签: android android-design-library android-tablayout


【解决方案1】:

您必须自己设置标题值

看到这个代码sn-p:

public void setupTabLayout(TabLayout tabLayout) {
    tabLayout.setTabMode(TabLayout.MODE_SCROLLABLE);
    tabLayout.setTabGravity(TabLayout.GRAVITY_CENTER);
    tabLayout.setupWithViewPager(mViewpager);

    TextView tab = (TextView) LayoutInflater.from(this).inflate(R.layout.custom_tab, null);
    tab.setText("Library");
    tab.setCompoundDrawablesWithIntrinsicBounds(0, R.drawable.ic_tabbar_library, 0, 0);
    tabLayout.getTabAt(0).setCustomView(tab);
    //..
}

custom_tab.xml

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/tab" />

也许这有帮助。

【讨论】:

  • 这种方式是不可能的。我需要将我的drawable设置为指示器而不是标签视图。