我创建了扩展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
时,没有连锁反应(因为它是LinearLayout)。所以我的问题是如何添加涟漪效应并附加onClick
侦听器?
答案 0 :(得分:0)
使用此代码在主布局上应用默认可选效果(Android 5.0 up)。
android:background="?android:attr/selectableItemBackground"