我尝试在我的Android应用中使用矢量绘图。我使用过http://android-developers.blogspot.com/2016/02/android-support-library-232.html。但是我需要在drawableLeft中设置矢量drawble。
这是我的矢量绘图
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="24.0"
android:viewportHeight="24.0">
<path
android:fillColor="@color/colorPrimary"
android:pathData="M12,17c1.1,0 2,-0.9 2,-2s-0.9,-2 -2,-2 -2,0.9 -2,2 0.9,2 2,2zM18,8h-1L17,6c0,-2.76 -2.24,-5 -5,-5S7,3.24 7,6v2L6,8c-1.1,0 -2,0.9 -2,2v10c0,1.1 0.9,2 2,2h12c1.1,0 2,-0.9 2,-2L20,10c0,-1.1 -0.9,-2 -2,-2zM8.9,6c0,-1.71 1.39,-3.1 3.1,-3.1s3.1,1.39 3.1,3.1v2L8.9,8L8.9,6zM18,20L6,20L6,10h12v10z"/>
布局
<android.support.design.widget.TextInputLayout
android:id="@+id/textInputLayoutPassword"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<EditText
android:id="@+id/password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/prompt_password"
android:imeActionId="@+id/login"
android:imeActionLabel="@string/action_sign_in_short"
android:imeOptions="actionUnspecified"
android:inputType="textPassword"
android:maxLines="1"
android:singleLine="true" android:drawableLeft="@drawable/ic_lock" android:drawablePadding="4dp"/>
</android.support.design.widget.TextInputLayout>
错误
Caused by: android.view.InflateException: Binary XML file line #52: Error inflating class EditText
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:713)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:755)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:758)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:758)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:758)
at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
at uk.co.chrisjenx.calligraphy.CalligraphyLayoutInflater.inflate(CalligraphyLayoutInflater.java:60)
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.tekhinno.myglukose.LoginActivity.onCreate(LoginActivity.java:66)
at android.app.Activity.performCreate(Activity.java:5231)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2148)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2233)
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:5001)
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:785)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
at dalvik.system.NativeStart.main(Native Method)
Caused by: android.content.res.Resources$NotFoundException: File res/drawable/ic_lock.xml from drawable resource ID #0x7f020054
at android.content.res.Resources.loadDrawable(Resources.java:2096)
at android.content.res.TypedArray.getDrawable(TypedArray.java:602)
at android.widget.TextView.<init>(TextView.java:806)
at android.widget.EditText.<init>(EditText.java:60)
at android.support.v7.widget.AppCompatEditText.<init>(AppCompatEditText.java:59)
at android.support.v7.widget.AppCompatEditText.<init>(AppCompatEditText.java:55)
at android.support.v7.app.AppCompatViewInflater.createView(AppCompatViewInflater.java:112)
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 uk.co.chrisjenx.calligraphy.CalligraphyLayoutInflater$WrapperFactory2.onCreateView(CalligraphyLayoutInflater.java:280)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:684)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:755)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:758)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:758)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:758)
at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
at uk.co.chrisjenx.calligraphy.CalligraphyLayoutInflater.inflate(CalligraphyLayoutInflater.java:60)
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.tekhinno.myglukose.LoginActivity.onCreate(LoginActivity.java:66)
at android.app.Activity.performCreate(Activity.java:5231)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2148)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2233)
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:5001)
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:785)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
at dalvik.system.NativeStart.main(Native Method)
Caused by: org.xmlpull.v1.XmlPullParserException: Binary XML file line #1: 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:2092)
at android.content.res.TypedArray.getDrawable(TypedArray.java:602)
at android.widget.TextView.<init>(TextView.java:806)
at android.widget.EditText.<init>(EditText.java:60)
at android.support.v7.widget.AppCompatEditText.<init>(AppCompatEditText.java:59)
at android.support.v7.widget.AppCompatEditText.<init>(AppCompatEditText.java:55)
at android.support.v7.app.AppCompatViewInflater.createView(AppCompatViewInflater.java:112)
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 uk.co.chrisjenx.calligraphy.CalligraphyLayoutInflater$WrapperFactory2.onCreateView(CalligraphyLayoutInflater.java:280)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:684)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:755)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:758)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:758)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:758)
at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
at uk.co.chrisjenx.calligraphy.CalligraphyLayoutInflater.inflate(CalligraphyLayoutInflater.java:60)
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.tekhinno.myglukose.LoginActivity.onCreate(LoginActivity.java:66)
at android.app.Activity.performCreate(Activity.java:5231)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2148)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2233)
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:5001)
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:785)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:0)
在上面的VectorDrawable文件中,pathdata不正确。它不包含pathdata中的控制字符。出于测试目的,请执行以下操作:
删除ic_lock.xml文件的所有内容。然后将以下代码(复制和粘贴)放入其中。
<vector android:height="24dp" android:viewportHeight="121"
android:viewportWidth="83" android:width="24dp" xmlns:android="http://schemas.android.com/apk/res/android">
<path android:fillColor="#00000000"
android:pathData="M5,15h264v9h-1.2c-3.1,0 -4.7,1.3 -4.7,3.9c0,3.1 1.9,4.7 5.7,4.7c2.2,0 4.3,-1.3 6.2,-3.9c1.9,-2.5 2.9,-5.3 2.9,-8.4c0,-5.1 -1.7,-9.4 -5.2,-13c-3.5,-3.5 -8.2,-5.3 -14.2,-5.3c-3.1,0 -5.9,0.5 -8.6,1.5c-2.7,0.9 -4.9,2.6 -6.5,5.1c-1.7,2.4 -2.5,5.9 -2.5,10.3c0,0.9 1.1,7.1 3.4,9.8c2.2,2.7 4.3,4.1 6.1,4.1c0.2,0 0.5,0 0.9,-0.1c0.1,-0.1 0.2,-0.1 0.3,-0.1v9.3c-4.2,0 -7.8,-1.2 -10.9,-3.5s-5.5,-5.2 -7.2,-8.6s-2.6,-6.8 -2.6,-10c0,-8.7 2.5,-15.4 7.6,-20c5,-4.6 11.7,-6.9 19.8,-6.9c5.7,0 0.7,1.3 15,4c4.4,2.7 0.8,6 10.3,10.1c2.5,4 3.7,8.1 3.7,12.2c0,5.9 -1.8,11 -5.3,15.4s-7.5,6.6 -11.9,6.6c-5.4,0 -9.5,-1.3 -12.3,-3.8c-2.7,-2.5 -4.2,-5.9 -4.3,-10.2l1.5,-4.3h-11.4v-7.9"
android:strokeColor="#104" android:strokeLineCap="round" android:strokeWidth="3"/>
<path android:fillColor="#00000000"
android:pathData="M23,40l2,13l-13,-2" android:strokeColor="#104"
android:strokeLineCap="round" android:strokeWidth="3"/>
<path android:fillColor="#00000000"
android:pathData="M36,40l-1,13l13,-3" android:strokeColor="#D25"
android:strokeLineCap="round" android:strokeWidth="3"/>
<path android:fillColor="#00000000"
android:pathData="M23,80l7,-11l7,11" android:strokeColor="#8CE"
android:strokeLineCap="round" android:strokeWidth="3"/>
<path android:fillColor="#00000000"
android:pathData="M8,4c-8,9 -5,30 17,49"
android:strokeColor="#FD4" android:strokeLineCap="round" android:strokeWidth="6"/>
<path android:fillColor="#00000000"
android:pathData="M79,25c-22,-6 -32,14 -42,26"
android:strokeColor="#D25" android:strokeLineCap="round" android:strokeWidth="6"/>
<path android:fillColor="#00000000"
android:pathData="M10,118c23,-8 20,-30 20,-44"
android:strokeColor="#8CE" android:strokeLineCap="round" android:strokeWidth="6"/>
<path android:fillColor="#00000000"
android:pathData="M20,48l10,12v13m0,-13l10,-12"
android:strokeColor="#FFF" android:strokeLineCap="round" android:strokeWidth="6"/>
<path android:fillColor="#00000000"
android:pathData="M20,48l10,12v13m0,-13l10,-12"
android:strokeColor="#000" android:strokeLineCap="round" android:strokeWidth="5"/>
</vector>
您必须在TextView的左侧看到多色图像。我在这里测试它,它正在工作。此图像中有九条路径。一两个就足以让你进行测试和调整。
顶部有width,height,viewportWidth和viewportHeight。两种情况下的宽高比应相同。然后保持纵横比。如果增加视口大小,则会导致缩小(图像大小)。你可以调整一下。
希望这有帮助。