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相同