創建自定義視圖。
創建一個類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>
謝謝!我現在就試試 – JaceAce