Android/自作画像view
提供: 初心者エンジニアの簡易メモ
- src/MyImageViewTestActivity.java
public class MyImageViewTestActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.imagelist_layout);
}
}
- src/MyImageView.java
public class MyImageView extends ImageView implements View.OnClickListener {
private Context mContext;
public MyImageView(Context context) {
super(context);
mContext = context;
setOnClickListener(this);
}
public MyImageView(Context context, AttributeSet attrs) {
super(context, attrs);
mContext = context;
setOnClickListener(this);
}
@Override
public void onClick(View v) {
Toast.makeText(mContext, "onclick", Toast.LENGTH_LONG).show();
}
}
- res/imagelist_layout.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <com.example.androidhelloworld.MyImageView android:layout_width="match_parent" android:layout_height="wrap_content" android:src="@drawable/ic_launcher" /> <com.example.androidhelloworld.MyImageView android:layout_width="match_parent" android:layout_height="wrap_content" android:src="@drawable/ic_launcher" /> </LinearLayout>
InflateException
android.view.InflateException: Binary XML file
なエラーが出るときはタグが絶対パスでない可能性がある。
<MyImageView
↓に変更してみる
<com.example.androidhelloworld.MyImageView
