Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/spring-mvc/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ssis/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 向自定义复合视图添加涟漪效果_Android_Android Custom View - Fatal编程技术网

Android 向自定义复合视图添加涟漪效果

Android 向自定义复合视图添加涟漪效果,android,android-custom-view,Android,Android Custom View,我创建了扩展LinearLayout的类: public class EventCategoryButton extends LinearLayout { private ImageView mIconImageView; private TextView mNameTextView; public EventCategoryButton(Context context) { super(context); initializeViews

我创建了扩展LinearLayout的类:

public class EventCategoryButton extends LinearLayout {
    private ImageView mIconImageView;
    private TextView mNameTextView;

    public EventCategoryButton(Context context) {
        super(context);
        initializeViews(context);
    }

    // Constructors with 2 and 3 arguments ...

    private void initializeViews(Context context) {
        setOrientation(LinearLayout.HORIZONTAL);
        setGravity(Gravity.CENTER_VERTICAL);
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        inflater.inflate(R.layout.component_event_category_button, this);
        mIconImageView = (ImageView) findViewById(R.id.event_category_icon);
        mNameTextView = (TextView) findViewById(R.id.event_category_name);
    }

    // Getters and setters...
}
和相应的布局文件:

<?xml version="1.0" encoding="utf-8"?>
<merge xmlns:android="http://schemas.android.com/apk/res/android">
    <ImageView
        android:id="@+id/event_category_icon"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="#0000"/>

    <TextView
        android:id="@+id/event_category_name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="20sp"
        android:background="#0000" />
</merge>

当我向服务器发出请求并获取类别时,我循环遍历每个类别并创建
EventCategoryButton
。这就是结果


但是当我点击这个
EventCategoryButton
时,没有任何连锁反应(因为它是线性布局)。因此,我的问题是如何添加涟漪效应并附加
onClick
listener?

使用此代码在主布局上应用默认的可选效果(Android 5.0以上)

android:background="?android:attr/selectableItemBackground"

我的主要布局是什么?我的
xml
文件中没有
LinearLayout
标记?@clzola您必须使用一些包装器布局,将内容包装到其中。因此,在该布局上,您可以应用此属性。我已将
ImageView
TextView
包装在
LinearLayout
中,并应用了
android:background=“?android:attr/selectableItemBackground”
,但没有任何效果,当我单击它时不会发生任何事情。。。