在API23中使用了TabLayout,在升级到API24进行编译后,程序运行时出现了如下错误:
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.demo/com.example.demo.activity.ViewPagerActivity}: android.view.InflateException: Binary XML file line #106: Binary XML file line #11: Error inflating class android.support.design.widget.TabLayout
下面是我xml中TabLayout的配置:
<android.support.design.widget.TabLayout
android:id="@+id/projecttabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/tv_project"
app:tabGravity="fill"
app:tabIndicatorHeight="1dp"
app:tabSelectedTextColor="@color/black"
app:tabTextAppearance="@style/CustomTabTextAppearance"
app:tabTextColor="@color/gray" />
styles:
<!--tab样式-->
<style name="CustomTabTextAppearance" parent="TextAppearance.Design.Tab">
<item name="android:textSize">10sp</item>
<item name="android:layout_width">match_parent</item>
<item name="android:layout_height">wrap_content</item>
<item name="android:textColor">@color/white</item>
<item name="textAllCaps">true</item>
</style>
注意:以上代码在API23中一切正常。在API24中运行时报错,需要使用Theme.AppCompat风格。
下面看解决方法:
1. style中声明:
<style name="AppBaseTheme" parent="Theme.AppCompat.Light">
</style>
<!-- Base application theme. -->
<style name="AppTheme" parent="AppBaseTheme">
<!-- Customize your theme here. -->
<!--<item name="android:windowNoTitle">true</item>-->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
</style>
2. 清单文件中声明application风格如下:
<application
android:name="com.xx.android.application.App"
android:allowBackup="true"
android:icon="@mipmap/xx"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme"
好了,经过以上两步就可以使用API24了。
效果如下: