如何更改tabLayout可点击的背景颜色?

时间:2017-11-15 09:57:47

标签: android android-tablayout

我知道可点击的背景可以制作一个选择器xml。但是它给了我以下错误(app:tabBackground):

引起:android.content.res.Resources $ NotFoundException:来自可绘制资源ID的文件res / color / tab_selector.xml#0x7f050062

我的布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.khash.copytimejava.MainActivity"
    android:orientation="vertical">


        <android.support.design.widget.TabLayout
            android:id="@+id/tabLayout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:tabMode="fixed"
            app:tabBackground="@color/tab_selector"
            app:tabTextColor="@android:color/white"
            app:tabTextAppearance="@style/TabLayoutTextStyle"
            app:tabSelectedTextColor="@android:color/white"/>

        <FrameLayout
            android:id="@+id/frame_container"
            android:layout_width="match_parent"
            android:layout_height="match_parent"/>

</LinearLayout>

我的标签选择器xml:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:color="@color/colorBlueWithAlpha" android:state_selected="true"></item>
    <item android:color="@color/colorBlueWithAlpha" android:state_pressed="true"></item>
    <item android:color="@color/colorPrimary"></item>
</selector>

当我改变

app:tabBackground="@color/tab_selector" 

app:tabBackground="@android:color/colorPrimary" 

它正常工作。

为什么它会给我.Resources $ NotFoundException?

enter image description here

移动可绘制文件夹(并命名为tab_color)后

错误日志:

FATAL EXCEPTION: main Process: com.example.khash.time, PID: 31922
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.khash.time/com.example.khash.time.MainActivity}: android.content.res.Resources$NotFoundException: File res/drawable/tab_color.xml from drawable resource ID #0x7f070075
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2548)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2607)
    at android.app.ActivityThread.access$900(ActivityThread.java:174)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1325)
    at android.os.Handler.dispatchMessage(Handler.java:102)
    at android.os.Looper.loop(Looper.java:146)
    at android.app.ActivityThread.main(ActivityThread.java:5756)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:515)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1291)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1107)
    at dalvik.system.NativeStart.main(Native Method)
Caused by: android.content.res.Resources$NotFoundException: File res/drawable/tab_color.xml from drawable resource ID #0x7f070075
    at android.content.res.Resources.loadDrawable(Resources.java:3457)
    at android.content.res.Resources.getDrawable(Resources.java:1897)
    at android.support.v4.content.ContextCompat.getDrawable(ContextCompat.java:353)
    at android.support.v7.widget.AppCompatDrawableManager.getDrawable(AppCompatDrawableManager.java:200)
    at android.support.v7.widget.AppCompatDrawableManager.getDrawable(AppCompatDrawableManager.java:188)
    at android.support.v7.content.res.AppCompatResources.getDrawable(AppCompatResources.java:100)
    at android.support.design.widget.TabLayout$TabView.<init>(TabLayout.java:1518)
    at android.support.design.widget.TabLayout.createTabView(TabLayout.java:932)
    at android.support.design.widget.TabLayout.newTab(TabLayout.java:569)
    at com.example.khash.time.MainActivity.setupTabLayout(MainActivity.java:44)
    at com.example.khash.time.MainActivity.onCreate(MainActivity.java:30)
    at android.app.Activity.performCreate(Activity.java:5605)
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1093)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2512)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2607)        
    at android.app.ActivityThread.access$900(ActivityThread.java:174) 
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1325) 
    at android.os.Handler.dispatchMessage(Handler.java:102) 
    at android.os.Looper.loop(Looper.java:146) 
    at android.app.ActivityThread.main(ActivityThread.java:5756) 
    at java.lang.reflect.Method.invokeNative(Native Method) 
    at java.lang.reflect.Method.invoke(Method.java:515) 
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1291) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1107) 
    at dalvik.system.NativeStart.main(Native Method) 
Caused by: org.xmlpull.v1.XmlPullParserException: Binary XML file line #0: <item> tag requires a 'drawable' attribute or child tag defining a drawable
    at android.graphics.drawable.StateListDrawable.inflate(StateListDrawable.java:181)
    at android.graphics.drawable.Drawable.createFromXmlInner(Drawable.java:990)
    at android.graphics.drawable.Drawable.createFromXml(Drawable.java:930)
    at android.content.res.Resources.loadDrawable(Resources.java:3453)
    at android.content.res.Resources.getDrawable(Resources.java:1897) 
    at android.support.v4.content.ContextCompat.getDrawable(ContextCompat.java:353) 
    at android.support.v7.widget.AppCompatDrawableManager.getDrawable(AppCompatDrawableManager.java:200) 
    at android.support.v7.widget.AppCompatDrawableManager.getDrawable(AppCompatDrawableManager.java:188) 
    at android.support.v7.content.res.AppCompatResources.getDrawable(AppCompatResources.java:100) 
    at android.support.design.widget.TabLayout$TabView.<init>(TabLayout.java:1518) 
    at android.support.design.widget.TabLayout.createTabView(TabLayout.java:932)         at android.support.design.widget.TabLayout.newTab(TabLayout.java:569) 
    at com.example.khash.time.MainActivity.setupTabLayout(MainActivity.java:44) 
    at com.example.khash.time.MainActivity.onCreate(MainActivity.java:30) 
    at android.app.Activity.performCreate(Activity.java:5605) 
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1093) 
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2512) 
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2607)
    at android.app.ActivityThread.access$900(ActivityThread.java:174) 
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1325) 
    at android.os.Handler.dispatchMessage(Handler.java:102) 
    at android.os.Looper.loop(Looper.java:146) 
    at android.app.ActivityThread.main(ActivityThread.java:5756) 
    at java.lang.reflect.Method.invokeNative(Native Method) 
    at java.lang.reflect.Method.invoke(Method.java:515) 
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1291) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1107) 
    at dalvik.system.NativeStart.main(Native Method) 

2 个答案:

答案 0 :(得分:1)

看起来android.support.design.widget.TabLayout不支持将颜色状态列表作为背景。

来自source code

if (mTabBackgroundResId != 0) {
    ViewCompat.setBackground(this, AppCompatResources.getDrawable(context, mTabBackgroundResId));
}

您可以使用StateListDrawable替换选择器中的android:color android:drawable来实现此目的。

答案 1 :(得分:-1)

步骤1:Buddy在res / values文件夹中创建一个xml文件,并将其命名为颜色。

第2步:在该文件中,颜色为:

<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="tab_selector">#008080</color>
</resources>

而不是#008080使用您选择的颜色。