简体   繁体   English

如何对按钮使用波纹效果

[英]how to use ripple effect for a button

I'm using the eclipse. 我正在使用日食。 I want to use ripple effect . 我想使用ripple effect

I have appcompat-v7:21.0.0. 我有appcompat-v7:21.0.0。

drawable/test.xml 绘制/的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 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+ ? 是否可以使用低于5.0+的标签?

You cannot use <ripple> in a drawable resource prior to Android 5.0, as it did not exist back then. 您不能在Android 5.0之前的可绘制资源中使用<ripple> ,因为当时不存在它。 Backports, like appcompat-v7 , can backport Java code and styles, but they cannot cause earlier versions of Android to recognize new resource structures. appcompat-v7这样的appcompat-v7移植可以appcompat-v7移植Java代码和样式,但是它们不能使早期版本的Android识别新的资源结构。

Also, RippleDrawable exists only in API Level 21 and does not appear to be in the appcompat-v7 backport. 另外, RippleDrawable仅存在于API级别21中,并且似乎不在appcompat-v7

You are welcome to try this RippleDrawable backport , though you probably have to use it from Java code, not from a resource. 欢迎您尝试RippleDrawable backport ,尽管您可能必须从Java代码而不是从资源中使用它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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