简体   繁体   中英

how to use ripple effect for a button

I'm using the eclipse. I want to use ripple effect .

I have appcompat-v7:21.0.0.

drawable/test.xml

<ripple xmlns:android="http://schemas.android.com/apk/res/android"
    android:color="@android:color/white"> <!-- ripple color -->

    <item android:drawable="@android:color/holo_blue_bright"/> <!-- normal color -->

</ripple>

activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.ex48_materialdesign.MainActivity" 
    android:orientation="vertical">


    <Button 
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="test"
        android:background="@drawable/test"
        />

</LinearLayout>

But I get these errors :

> 03-12 15:12:11.441: E/AndroidRuntime(4828): FATAL EXCEPTION: main
> 03-12 15:12:11.441: E/AndroidRuntime(4828):
> java.lang.RuntimeException: Unable to start activity
> ComponentInfo{com.example.ex48_materialdesign/com.example.ex48_materialdesign.MainActivity}:
> android.view.InflateException: Binary XML file line #13: Error
> inflating class android.widget.Button 03-12 15:12:11.441:
> E/AndroidRuntime(4828):   at
> android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2247)
> 03-12 15:12:11.441: E/AndroidRuntime(4828):   at
> android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2297)
> 03-12 15:12:11.441: E/AndroidRuntime(4828):   at
> android.app.ActivityThread.access$700(ActivityThread.java:152) 03-12
> 15:12:11.441: E/AndroidRuntime(4828):     at
> android.app.ActivityThread$H.handleMessage(ActivityThread.java:1282)
> 03-12 15:12:11.441: E/AndroidRuntime(4828):   at
> android.os.Handler.dispatchMessage(Handler.java:99) 03-12
> 15:12:11.441: E/AndroidRuntime(4828):     at
> android.os.Looper.loop(Looper.java:137) 03-12 15:12:11.441:
> E/AndroidRuntime(4828):   at
> android.app.ActivityThread.main(ActivityThread.java:5328) 03-12
> 15:12:11.441: E/AndroidRuntime(4828):     at
> java.lang.reflect.Method.invokeNative(Native Method) 03-12
> 15:12:11.441: E/AndroidRuntime(4828):     at
> java.lang.reflect.Method.invoke(Method.java:511) 03-12 15:12:11.441:
> E/AndroidRuntime(4828):   at
> com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)
> 03-12 15:12:11.441: E/AndroidRuntime(4828):   at
> com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869) 03-12
> 15:12:11.441: E/AndroidRuntime(4828):     at
> dalvik.system.NativeStart.main(Native Method) 03-12 15:12:11.441:
> E/AndroidRuntime(4828): Caused by: android.view.InflateException:
> Binary XML file line #13: Error inflating class android.widget.Button
> 03-12 15:12:11.441: E/AndroidRuntime(4828):   at
> android.view.LayoutInflater.createView(LayoutInflater.java:619) 03-12
> 15:12:11.441: E/AndroidRuntime(4828):     at
> com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:56)
> 03-12 15:12:11.441: E/AndroidRuntime(4828):   at
> android.view.LayoutInflater.onCreateView(LayoutInflater.java:666)
> 03-12 15:12:11.441: E/AndroidRuntime(4828):   at
> android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:691)
> 03-12 15:12:11.441: E/AndroidRuntime(4828):   at
> android.view.LayoutInflater.rInflate(LayoutInflater.java:752) 03-12
> 15:12:11.441: E/AndroidRuntime(4828):     at
> android.view.LayoutInflater.inflate(LayoutInflater.java:495) 03-12
> 15:12:11.441: E/AndroidRuntime(4828):     at
> android.view.LayoutInflater.inflate(LayoutInflater.java:397) 03-12
> 15:12:11.441: E/AndroidRuntime(4828):     at
> android.view.LayoutInflater.inflate(LayoutInflater.java:353) 03-12
> 15:12:11.441: E/AndroidRuntime(4828):     at
> android.support.v7.app.ActionBarActivityDelegateBase.setContentView(ActionBarActivityDelegateBase.java:228)
> 03-12 15:12:11.441: E/AndroidRuntime(4828):   at
> android.support.v7.app.ActionBarActivity.setContentView(ActionBarActivity.java:102)
> 03-12 15:12:11.441: E/AndroidRuntime(4828):   at
> com.example.ex48_materialdesign.MainActivity.onCreate(MainActivity.java:13)
> 03-12 15:12:11.441: E/AndroidRuntime(4828):   at
> android.app.Activity.performCreate(Activity.java:5250) 03-12
> 15:12:11.441: E/AndroidRuntime(4828):     at
> android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1097)
> 03-12 15:12:11.441: E/AndroidRuntime(4828):   at
> android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)
> 03-12 15:12:11.441: E/AndroidRuntime(4828):   ... 11 more 03-12
> 15:12:11.441: E/AndroidRuntime(4828): Caused by:
> java.lang.reflect.InvocationTargetException 03-12 15:12:11.441:
> E/AndroidRuntime(4828):   at
> java.lang.reflect.Constructor.constructNative(Native Method) 03-12
> 15:12:11.441: E/AndroidRuntime(4828):     at
> java.lang.reflect.Constructor.newInstance(Constructor.java:417) 03-12
> 15:12:11.441: E/AndroidRuntime(4828):     at
> android.view.LayoutInflater.createView(LayoutInflater.java:593) 03-12
> 15:12:11.441: E/AndroidRuntime(4828):     ... 24 more 03-12 15:12:11.441:
> E/AndroidRuntime(4828): Caused by:
> android.content.res.Resources$NotFoundException: File
> res/drawable/test.xml from drawable resource ID #0x7f020034 03-12
> 15:12:11.441: E/AndroidRuntime(4828):     at
> android.content.res.Resources.loadDrawable(Resources.java:2842) 03-12
> 15:12:11.441: E/AndroidRuntime(4828):     at
> android.content.res.TypedArray.getDrawable(TypedArray.java:602) 03-12
> 15:12:11.441: E/AndroidRuntime(4828):     at
> android.view.View.<init>(View.java:3460) 03-12 15:12:11.441:
> E/AndroidRuntime(4828):   at
> android.widget.TextView.<init>(TextView.java:791) 03-12 15:12:11.441:
> E/AndroidRuntime(4828):   at
> android.widget.Button.<init>(Button.java:107) 03-12 15:12:11.441:
> E/AndroidRuntime(4828):   at
> android.widget.Button.<init>(Button.java:103) 03-12 15:12:11.441:
> E/AndroidRuntime(4828):   ... 27 more 03-12 15:12:11.441:
> E/AndroidRuntime(4828): Caused by:
> org.xmlpull.v1.XmlPullParserException: Binary XML file line #1:
> invalid drawable tag ripple 03-12 15:12:11.441:
> E/AndroidRuntime(4828):   at
> android.graphics.drawable.Drawable.createFromXmlInner(Drawable.java:917)
> 03-12 15:12:11.441: E/AndroidRuntime(4828):   at
> android.graphics.drawable.Drawable.createFromXml(Drawable.java:858)
> 03-12 15:12:11.441: E/AndroidRuntime(4828):   at
> android.content.res.Resources.loadDrawable(Resources.java:2839) 03-12
> 15:12:11.441: E/AndroidRuntime(4828):     ... 32 more

is it possible to use tag for lower than 5.0+ ?

You cannot use <ripple> in a drawable resource prior to Android 5.0, as it did not exist back then. Backports, like appcompat-v7 , can backport Java code and styles, but they cannot cause earlier versions of Android to recognize new resource structures.

Also, RippleDrawable exists only in API Level 21 and does not appear to be in the appcompat-v7 backport.

You are welcome to try this RippleDrawable backport , though you probably have to use it from Java code, not from a resource.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM