Android Studio3.5,可能是新版本Android,无法使用android.support.design.widget.TabLayout,因此选择com.google.android.material.tabs.TabLayout。
如果是xml文件中报错,首先注意MainActivity中引入的是哪个tablayout包,我发现自己引入的是Google的,因此在xml中也使用了Google的tablayout,解决了错误。
配置文件中引入:
implementation 'com.android.support:support-v4:29.0.2' implementation 'com.android.support:design:29.0.2'
activity_main.xml中部分代码
<com.google.android.material.tabs.TabLayout
android:id="@+id/tabLayout"
android:layout_width="match_parent"
android:layout_height="80dp"
android:background="#ffffff"
app:tabIndicatorColor="@color/design_default_color_primary"
app:tabIndicatorHeight="8dp"
app:tabSelectedTextColor="#000000"
app:tabMode="fixed"
app:tabBackground="@drawable/selected"
/>
其余使用与android.support.design.widget.TabLayout相同