创建自定义视图。
创建一个类 Widget 并使用线性布局扩展它。在构造函数中填充包含您的布局(图像视图和 3 个文本视图)的布局。
通过调用 addView 将膨胀的视图添加到类本身
编写 setter 函数,您将在其中接受包含与事件相关的信息的事件类对象,并且该函数只需将值设置为适当的文本视图。
Class Widget extends LinearLayout{
private TextView artistName;
public Widget(Context context) {
super(context);
View view = LayoutInflater.from(getContext()).inflate(
R.layout.widget, null);
artistName = view.findViewById(R.id.artistName);
this.addView(view);
}
public void setName(Event event){
artistName.setText(event.getArtistName());
}
}
在您的活动布局中,您可以将其用作
<pkg_name.Widget android:id="@+id/widget1"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
</pkg_name.Widget>