美文网首页
图片选择器

图片选择器

作者: Android_冯星 | 来源:发表于2019-03-07 15:45 被阅读0次

    TextView中textColor属性 报错

    代码:

    <TextView
                android:id="@+id/select_album"
                android:layout_width="wrap_content"
                android:layout_height="?actionBarSize"
                android:drawableRight="@drawable/ic_arrow_drop_down_white_24dp"
                android:foreground="?selectableItemBackground"
                android:gravity="center"
                android:textColor="?attr/album.element.color"
                android:textSize="20sp" />
    

    attr:

    <attr name="album.element.color" format="reference|color"/>
    
    FATAL EXCEPTION: main
                                                       Process: cn.fengxing.com.mcppictureselect, PID: 31417
                                                       java.lang.RuntimeException: Unable to start activity ComponentInfo{cn.fengxing.com.mcppictureselect/cn.fengxing.com.mcppictureselect.MainActivity}: android.view.InflateException: Binary XML file line #19: Binary XML file line #19: Error inflating class TextView
                                                           at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3174)
                                                           at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3269)
                                                           at android.app.ActivityThread.-wrap12(Unknown Source:0)
                                                           at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1881)
                                                           at android.os.Handler.dispatchMessage(Handler.java:108)
                                                           at android.os.Looper.loop(Looper.java:166)
                                                           at android.app.ActivityThread.main(ActivityThread.java:7380)
                                                           at java.lang.reflect.Method.invoke(Native Method)
                                                           at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:245)
                                                           at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:926)
                                                        Caused by: android.view.InflateException: Binary XML file line #19: Binary XML file line #19: Error inflating class TextView
                                                        Caused by: android.view.InflateException: Binary XML file line #19: Error inflating class TextView
                                                        Caused by: java.lang.UnsupportedOperationException: Failed to resolve attribute at index 5: TypedValue{t=0x2/d=0x7f010000 a=-1}
                                                           at android.content.res.TypedArray.getColorStateList(TypedArray.java:569)
                                                           at android.widget.TextView.<init>(TextView.java:1291)
                                                           at android.widget.TextView.<init>(TextView.java:872)
                                                           at android.support.v7.widget.AppCompatTextView.<init>(AppCompatTextView.java:62)
                                                           at android.support.v7.widget.AppCompatTextView.<init>(AppCompatTextView.java:58)
                                                           at android.support.v7.app.AppCompatViewInflater.createView(AppCompatViewInflater.java:103)
                                                           at android.support.v7.app.AppCompatDelegateImplV9.createView(AppCompatDelegateImplV9.java:1029)
                                                           at android.support.v7.app.AppCompatDelegateImplV9.onCreateView(AppCompatDelegateImplV9.java:1087)
                                                           at android.support.v4.view.LayoutInflaterCompatHC$FactoryWrapperHC.onCreateView(LayoutInflaterCompatHC.java:47)
                                                           at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:783)
                                                           at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:741)
                                                           at android.view.LayoutInflater.rInflate(LayoutInflater.java:874)
                                                           at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:835)
                                                           at android.view.LayoutInflater.rInflate(LayoutInflater.java:877)
                                                           at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:835)
                                                           at android.view.LayoutInflater.inflate(LayoutInflater.java:515)
                                                           at android.view.LayoutInflater.inflate(LayoutInflater.java:423)
                                                           at android.view.LayoutInflater.inflate(LayoutInflater.java:374)
                                                           at android.support.v7.app.AppCompatDelegateImplV9.setContentView(AppCompatDelegateImplV9.java:292)
                                                           at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:140)
                                                           at cn.fengxing.com.mcppictureselect.MainActivity.onCreate(MainActivity.java:11)
                                                           at android.app.Activity.performCreate(Activity.java:7352)
                                                           at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1218)
                                                           at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3127)
                                                           at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3269)
                                                           at android.app.ActivityThread.-wrap12(Unknown Source:0)
                                                           at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1881)
                                                           at android.os.Handler.dispatchMessage(Handler.java:108)
                                                           at android.os.Looper.loop(Looper.java:166)
                                                           at android.app.ActivityThread.main(ActivityThread.java:7380)
                                                           at java.lang.reflect.Method.invoke(Native Method)
                                                           at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:245)
                                                           at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:926)
    

    原因是TypeArray转换是没有找到值,导致的。
    解决办法:我给直接给定了颜色值。
    不是好的解决方案:

    <TextView
                android:id="@+id/select_album"
                android:layout_width="wrap_content"
                android:layout_height="?actionBarSize"
                android:drawableRight="@drawable/ic_arrow_drop_down_white_24dp"
                android:foreground="?selectableItemBackground"
                android:gravity="center"
                android:textColor="@android:color/white"
                android:textSize="20sp" />
    

    前景色 foreground

    android:foreground="?selectableItemBackground"

    foreground 也就是前景色,它与background相对应,顾名思义,它指定的drawable是在view视图的上方绘制的。

    image.png

    在布局中使用?attr

    1. 在布局文件中定义 例如textColor
    <TextView
                android:id="@+id/button_preview"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="start"
                android:foreground="?selectableItemBackground"
                android:padding="16dp"
                android:textColor="?attr/bottomToolbar.preview.textColor"
                android:text="@string/button_preview"
                android:textSize="16sp" />
    
    1. 在attr文件中定义
    <attr name="bottomToolbar.preview.textColor" format="reference|color" />
    
    1. 在style样式中定义
    <item name="bottomToolbar.preview.textColor">@color/bottom_toolbar_bg</item>
    
    1. 在color中定义
    <color name="bottom_toolbar_bg">#FFFFFF</color>
    

    完成这四个步骤,在布局文件中使用?attr就是使用到对应的color文件中的颜色。

    相关文章

      网友评论

          本文标题:图片选择器

          本文链接:https://www.haomeiwen.com/subject/yzhxcftx.html