在用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)
1
2
3
4
5
6
|
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'
}
|
1
2
3
4
5
6
7
|
<linearlayout android:layout_height=
"match_parent"
android:layout_width=
"match_parent"
android:orientation=
"vertical"
xmlns:android=
"http://schemas.android.com/apk/res/android"
xmlns:app=
"http://schemas.android.com/apk/res-auto"
>
</android.support.v4.view.viewpager></android.support.design.widget.tablayout></linearlayout>
|
错误信息提示:
1
2
3
4
5
6
7
8
9
10
11
|
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
)
|
1
|
setContentView(R.layout.activity_main);
|
解决错误的方法:
在res/values中的styles.xml中,需要修改parent的属性值,不能使用自定义的。
这里面有两个styles.xml都需要修改
修改内容,参考如下:
1
2
3
4
5
6
|
<resources><style name=
"AppTheme"
parent=
"Base.AppTheme"
type=
"text/css"
><!-- Customize your theme here. --></style>
<!-- Base application theme. --><style name=
"Base.AppTheme"
parent=
"Theme.AppCompat.Light.DarkActionBar"
type=
"text/css"
><item name=
"colorPrimary"
>#673AB7</item>
<item name=
"colorPrimaryDark"
>#512DA8</item>
<item name=
"colorAccent"
>#FF4081</item></style>
</resources>
|
1
2
|
<resources><style name=
"AppTheme"
parent=
"Base.AppTheme"
type=
"text/css"
></style>
</resources>
|
结果就成功解决了。
类似问题参考链接:http://stackoverflow.com/questions/30547323/error-when-using-any-android-design-support-library-elements/30557995#30557995