Android 向自定义复合视图添加涟漪效果
我创建了扩展LinearLayout的类: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
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”
,但没有任何效果,当我单击它时不会发生任何事情。。。