如何为Android 5.0版中的EditText提供自定义样式

时间:2018-04-11 11:23:52

标签: android android-edittext android-custom-view

在我的应用程序中,我使用以下代码来设置EditText和Buttons的样式。

round_corner_edittext.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item>
        <shape android:shape="rectangle">
            <solid android:color="#ffffff"/>
            <corners android:radius="10dp" />
            <stroke
                android:width="2dp"
                android:color="#3bbdfa"
                />
        </shape>
    </item>
</selector>

button_states.xml

<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:state_pressed="false">
        <shape android:shape="rectangle">
            <corners android:radius="10dp" />
            <solid android:color="#41ba7a" />
            <stroke
                android:width="2dip"
                android:color="#03ae3c" />
            <padding
                android:bottom="4dp"
                android:left="4dp"
                android:right="4dp"
                android:top="4dp" />
        </shape>
    </item>
    <item android:state_pressed="true">
        <shape android:shape="rectangle">
            <corners android:radius="10dp" />
            <solid android:color="#3AA76D" />
            <stroke
                android:width="2dip"
                android:color="#03ae3c" />
            <padding
                android:bottom="4dp"
                android:left="4dp"
                android:right="4dp"
                android:top="4dp" />
        </shape>
    </item>

</selector>

这两个代码在Android版牛轧糖中完美运行。但是当我在棒棒糖中运行它时它不起作用,而我的应用程序将崩溃 任何人都帮我解决这个问题。

以下是Logcat:

E/AndroidRuntime: FATAL EXCEPTION: main
                  Process: com.example.system.smstemplates, PID: 7405
                  java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.system.smstemplates/com.example.system.smstemplates.SmsActivity}: android.view.InflateException: Binary XML file line #0: Error inflating class EditText
                      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2325)
                      at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387)
                      at android.app.ActivityThread.access$800(ActivityThread.java:151)
                      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
                      at android.os.Handler.dispatchMessage(Handler.java:102)
                      at android.os.Looper.loop(Looper.java:135)
                      at android.app.ActivityThread.main(ActivityThread.java:5254)
                      at java.lang.reflect.Method.invoke(Native Method)
                      at java.lang.reflect.Method.invoke(Method.java:372)
                      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
                   Caused by: android.view.InflateException: Binary XML file line #0: Error inflating class EditText
                      at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:763)
                      at android.view.LayoutInflater.rInflate(LayoutInflater.java:806)
                      at android.view.LayoutInflater.rInflate(LayoutInflater.java:809)
                      at android.view.LayoutInflater.rInflate(LayoutInflater.java:809)
                      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 android.support.v7.app.AppCompatDelegateImplV9.setContentView(AppCompatDelegateImplV9.java:287)
                      at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:139)
                      at com.example.system.smstemplates.SmsActivity.onCreate(SmsActivity.java:24)
                      at android.app.Activity.performCreate(Activity.java:5990)
                      at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)
                      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2278)
                      at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387) 
                      at android.app.ActivityThread.access$800(ActivityThread.java:151) 
                      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303) 
                      at android.os.Handler.dispatchMessage(Handler.java:102) 
                      at android.os.Looper.loop(Looper.java:135) 
                      at android.app.ActivityThread.main(ActivityThread.java:5254) 
                      at java.lang.reflect.Method.invoke(Native Method) 
                      at java.lang.reflect.Method.invoke(Method.java:372) 
                      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903) 
                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698) 
                   Caused by: android.content.res.Resources$NotFoundException: Resource "com.example.system.smstemplates:drawable/rounded_border_edittext" (7f060069)  is not a Drawable (color or path): TypedValue{t=0x1/d=0x7f060069 a=-1 r=0x7f060069}
                      at android.content.res.Resources.loadDrawableForCookie(Resources.java:2441)
                      at android.content.res.Resources.loadDrawable(Resources.java:2381)
                      at android.content.res.TypedArray.getDrawable(TypedArray.java:749)
                      at android.view.View.<init>(View.java:3737)
                      at android.widget.TextView.<init>(TextView.java:637)
                      at android.widget.EditText.<init>(EditText.java:65)
                      at android.widget.EditText.<init>(EditText.java:61)
                      at android.support.v7.widget.AppCompatEditText.<init>(AppCompatEditText.java:62)
                      at android.support.v7.widget.AppCompatEditText.<init>(AppCompatEditText.java:58)
                      at android.support.v7.app.AppCompatViewInflater.createView(AppCompatViewInflater.java:112)
                      at android.support.v7.app.AppCompatDelegateImplV9.createView(AppCompatDelegateImplV9.java:1024)
                      at android.support.v7.app.AppCompatDelegateImplV9.onCreateView(AppCompatDelegateImplV9.java:1081)
                      at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:725)
                      at android.view.LayoutInflater.rInflate(LayoutInflater.java:806) 
                      at android.view.LayoutInflater.rInflate(LayoutInflater.java:809) 
                      at android.view.LayoutInflater.rInflate(LayoutInflater.java:809) 
                      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 android.support.v7.app.AppCompatDelegateImplV9.setContentView(AppCompatDelegateImplV9.java:287) 
                      at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:139) 
                      at com.example.system.smstemplates.SmsActivity.onCreate(SmsActivity.java:24) 
                      at android.app.Activity.performCreate(Activity.java:5990) 
                      at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106) 
                      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2278) 
                      at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387) 
                      at android.app.ActivityThread.access$800(ActivityThread.java:151) 
                      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303) 
                      at android.os.Handler.dispatchMessage(Handler.java:102) 
                      at android.os.Looper.loop(Looper.java:135) 
                      at android.app.ActivityThread.main(ActivityThread.java:5254) 
                      at java.lang.reflect.Method.invoke(Native Method) 
                      at java.lang.reflect.Method.invoke(Method.java:372) 
                      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903) 
                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698) 

0 个答案:

没有答案