在用Android Studio开发的过程中,一遇到废弃、不被推荐的方法和类,我就想做点什么去掉上面的横线。然后,被一个不是问题的问题困扰了很久。
之前我们在创建固定Tabs的时候,类似于这样的功能,
使用的是ActionBar.Tab但是ActionBar deprecated。那什么是新的解决方案呢?TabLayout+ViewPager就能实现上述的功能
出现的错误:android.view.InflateException: Binary XML file line #8: Error inflating class android.support.design
首先,我明确一下,我的sdk升级到了21版本以上。support Library 也是最新的。
在Android studio添加依赖
build.gradle(Module.app)
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:22.2.0'
compile 'com.android.support:support-v4:22.2.0'
compile 'com.android.support:design:22.2.0'
}
activity_main.xml内容如下:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<android.support.design.widget.TabLayout
android:id="@+id/sliding_tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:tabMode="scrollable" />
<android.support.v4.view.ViewPager
android:id="@+id/viewpager"
android:layout_width="match_parent"
android:layout_height="0px"
android:layout_weight="1"
android:background="@android:color/white" />
</LinearLayout>
错误信息提示:
Caused by: android.view.InflateException: Binary XML file line #8: Error inflating class android.support.design.widget.TabLayout
at android.view.LayoutInflater.createView(LayoutInflater.java:633)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:743)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:806)
at android.view.LayoutInflater.inflate(LayoutInflater.java:504)
at android.view.LayoutInflater.inflate(LayoutInflater.java:414)
at android.view.LayoutInflater.inflate(LayoutInflater.java:365)
at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:377)
at android.app.Activity.setContentView(Activity.java:2144)
at com.example.hp.myapplication.MainActivity.onCreate(MainActivity.java:15)
at android.app.Activity.performCreate(Activity.java:5933)
上面提示的错误是,在xml文件中的第8行,出现了Inflate错误,然后导致MainActivity.java:15行中的
setContentView(R.layout.activity_main);错误。
解决错误的方法:
在res/values中的styles.xml中,需要修改parent的属性值,不能使用自定义的。
这里面有两个styles.xml都需要修改
修改内容,参考如下:
<resources>
<style name="AppTheme" parent="Base.AppTheme">
<!-- Customize your theme here. -->
</style>
<!-- Base application theme. -->
<style name="Base.AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="colorPrimary">#673AB7</item>
<item name="colorPrimaryDark">#512DA8</item>
<item name="colorAccent">#FF4081</item>
</style>
</resources>
修改parent中的属性值,与上面定义的对应
<resources>
<style name="AppTheme" parent="Base.AppTheme">
</style>
</resources>
结果就成功解决了。