当android:drawableTop被添加到xml文件

时间:2016-11-09 12:09:48

标签: android xml android-layout textview android-vectordrawable

我正在制作一个xml文件,其中包含一个带星级计数器的评分系统 - 它计算用户拥有的星数。现在这是my_profile.xml文件。在我的相对布局中,我想从矢量绘图中添加一个星形符号。我添加的这行是:

    android:drawableTop="@drawable/ic_icon_star"

我的textview看起来像这样:

<RelativeLayout 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=".navdraweractivities.MyProfileActivity">
    ...
    <TextView
    android:id="@+id/textViewStars"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBaseline="@+id/buttonEdit"
    android:layout_alignBottom="@+id/buttonEdit"
    android:layout_alignStart="@+id/textViewAbout"
    android:drawableTop="@drawable/ic_icon_star"
    android:gravity="center"
    android:text="0"
    android:textColor="#FFFFFF"
    android:textSize="18sp" />
    ...
</RelativeLayout>

ic_icon_star在我将其添加到imageView时有效。我尝试添加不同的drawables和drawableLeft,drawableRight等,但结果相同。

logcat的:

  11-09 13:15:42.469 31754-31754/? E/AndroidRuntime: FATAL EXCEPTION: main
                                               Process: com.weekendcoder.kemo.restaraunt, PID: 31754
                                               java.lang.RuntimeException: Unable to start activity ComponentInfo{com.weekendcoder.kemo.restaraunt/com.weekendcoder.kemo.restaraunt.navdraweractivities.MyProfileActivity}: android.view.InflateException: Binary XML file line #85: Error inflating class TextView
                                                   at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
                                                   at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
                                                   at android.app.ActivityThread.access$800(ActivityThread.java:135)
                                                   at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
                                                   at android.os.Handler.dispatchMessage(Handler.java:102)
                                                   at android.os.Looper.loop(Looper.java:136)
                                                   at android.app.ActivityThread.main(ActivityThread.java:5021)
                                                   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:827)
                                                   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:643)
                                                   at dalvik.system.NativeStart.main(Native Method)
                                                Caused by: android.view.InflateException: Binary XML file line #85: Error inflating class TextView
                                                   at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:713)
                                                   at android.view.LayoutInflater.rInflate(LayoutInflater.java:755)
                                                   at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
                                                   at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
                                                   at android.view.LayoutInflater.inflate(LayoutInflater.java:353)
                                                   at android.support.v7.app.AppCompatDelegateImplV9.setContentView(AppCompatDelegateImplV9.java:284)
                                                   at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:140)
                                                   at com.weekendcoder.kemo.restaraunt.navdraweractivities.MyProfileActivity.onCreate(MyProfileActivity.java:26)
                                                   at android.app.Activity.performCreate(Activity.java:5336)
                                                   at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1090)
                                                   at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
                                                   at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245) 
                                                   at android.app.ActivityThread.access$800(ActivityThread.java:135) 
                                                   at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196) 
                                                   at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                   at android.os.Looper.loop(Looper.java:136) 
                                                   at android.app.ActivityThread.main(ActivityThread.java:5021) 
                                                   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:827) 
                                                   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:643) 
                                                   at dalvik.system.NativeStart.main(Native Method) 
                                                Caused by: android.content.res.Resources$NotFoundException: File res/drawable/ic_icon_star.xml from drawable resource ID #0x7f020090
                                                   at android.content.res.Resources.loadDrawable(Resources.java:2131)
                                                   at android.content.res.TypedArray.getDrawable(TypedArray.java:602)
                                                   at android.widget.TextView.<init>(TextView.java:810)
                                                   at android.support.v7.widget.AppCompatTextView.<init>(AppCompatTextView.java:59)
                                                   at android.support.v7.widget.AppCompatTextView.<init>(AppCompatTextView.java:55)
                                                   at android.support.v7.app.AppCompatViewInflater.createView(AppCompatViewInflater.java:103)
                                                   at android.support.v7.app.AppCompatDelegateImplV9.createView(AppCompatDelegateImplV9.java:1013)
                                                   at android.support.v7.app.AppCompatDelegateImplV9.onCreateView(AppCompatDelegateImplV9.java:1072)
                                                   at android.support.v4.view.LayoutInflaterCompatHC$FactoryWrapperHC.onCreateView(LayoutInflaterCompatHC.java:44)
                                                   at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:684)
                                                   at android.view.LayoutInflater.rInflate(LayoutInflater.java:755) 
                                                   at android.view.LayoutInflater.inflate(LayoutInflater.java:492) 
                                                   at android.view.LayoutInflater.inflate(LayoutInflater.java:397) 
                                                   at android.view.LayoutInflater.inflate(LayoutInflater.java:353) 
                                                   at android.support.v7.app.AppCompatDelegateImplV9.setContentView(AppCompatDelegateImplV9.java:284) 
                                                   at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:140) 
                                                   at com.weekendcoder.kemo.restaraunt.navdraweractivities.MyProfileActivity.onCreate(MyProfileActivity.java:26) 
                                                   at android.app.Activity.performCreate(Activity.java:5336) 
                                                   at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1090) 
                                                   at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159) 
                                                   at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245) 
                                                   at android.app.ActivityThread.access$800(ActivityThread.java:135) 
                                                   at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196) 
                                                   at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                   at android.os.Looper.loop(Looper.java:136) 
                                                   at android.app.ActivityThread.main(ActivityThread.java:5021) 
                                                   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:827) 
                                                   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:643) 
                                                   at dalvik.system.NativeStart.main(Native Method) 
                                                Caused by: org.xmlpull.v1.XmlPullParserException: Binary XML file line #5: invalid drawable tag vector
                                                   at android.graphics.drawable.Drawable.createFromXmlInner(Drawable.java:933)
                                                   at android.graphics.drawable.Drawable.createFromXml(Drawable.java:877)
                                                   at android.content.res.Resources.loadDrawable(Resources.java:2127)
                                                   at android.content.res.TypedArray.getDrawable(TypedArray.java:602) 
                                                   at android.widget.TextView.<init>(TextView.java:810) 
                                                   at android.support.v7.widget.AppCompatTextView.<init>(AppCompatTextView.java:59) 
                                                   at android.support.v7.widget.AppCompatTextView.<init>(AppCompatTextView.java:55) 
                                                   at android.support.v7.app.AppCompatViewInflater.createView(AppCompatViewInflater.java:103) 
                                                   at android.support.v7.app.AppCompatDelegateImplV9.createView(AppCompatDelegateImplV9.java:1013) 
                                                   at android.support.v7.app.AppCompatDelegateImplV9.onCreateView(AppCompatDelegateImplV9.java:1072) 
                                                   at android.support.v4.view.LayoutInflaterCompatHC$FactoryWrapperHC.onCreateView(LayoutInflaterCompatHC.java:44) 
                                                   at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:684) 
                                                   at android.view.LayoutInflater.rInflate(LayoutInflater.java:755) 
                                                   at android.view.LayoutInflater.inflate(LayoutInflater.java:492) 
                                                   at android.view.LayoutInflater.inflate(LayoutInflater.java:397) 
                                                   at android.view.LayoutInflater.inflate(LayoutInflater.java:353) 
                                                   at android.support.v7.app.AppCompatDelegateImplV9.setContentView(AppCompatDelegateImplV9.java:284) 
                                                   at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:140) 
                                                   at com.weekendcoder.kemo.restaraunt.navdraweractivities.MyProfileActivity.onCreate(MyProfileActivity.java:26) 
                                                   at android.app.Activity.performCreate(Activity.java:5336) 
                                                   at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1090) 
                                                   at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159) 
                                                   at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245) 
                                                   at android.app.ActivityThread.access$800(ActivityThread.java:135) 
                                                   at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196) 
                                                   at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                   at android.os.Looper.loop(Looper.java:136) 
                                                   at android.app.ActivityThread.main(ActivityThread.java:5021) 
                                                   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:827) 
                                                   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:643) 
                                                   at dalvik.system.NativeStart.main(Native Method) 

1 个答案:

答案 0 :(得分:1)

在下面的链接中,请参阅帖子

上的strik文本
  

通过使用此间接,您可以在这种情况下使用矢量drawable   作为TextView的 android:drawableLeft 属性,不会   通常能够支持矢量绘图。

http://android-developers.blogspot.in/2016/02/android-support-library-232.html