xmlを使用せずにプログラムでonclickリスナーを介してdrawablesフォルダーから画像を取得できるボタンを取得するにはどうすればよいですか。キャンバス上にボタンとシェイプを作成しましたが、ボタンを onclick にすることができません...アイデアはありますか?
1620 次
5 に答える
1
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
imageview.setImageResource(R.drawable.image);
}
});
これが役立つと思います。
于 2012-08-23T08:30:04.383 に答える
0
ImageButton を使用して、背景をドローアブルから取得した画像として設定しないのはなぜですか? そんな感じ
ImageButton imgBtn = (ImageButton) findViewById(....)
imgBtn.setBackgroundResource(R.your_resource_id)
imgBtn.setOnclickListener(.......)
これがお役に立てば幸いです。
于 2012-08-22T21:38:03.007 に答える
0
最初に、ユーザーがボタンをクリックするたびにトリガーされる機能を設定する必要があります。これは、レイアウト xml ファイル内のボタンのプロパティで行います。
android:onClick="someFunction"
ビューがバインドされているアクティビティ内にこの関数を作成する必要があります。
public void someFunction(View v){
// Load the drawable
}
于 2012-08-22T21:00:09.213 に答える
0
LinearLayout mLinearLayout;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Create a LinearLayout in which to add the ImageView
mLinearLayout = new LinearLayout(this);
// Instantiate an ImageView and define its properties
ImageView i = new ImageView(this);
i.setImageResource(R.drawable.my_image);
i.setAdjustViewBounds(true); // set the ImageView bounds to match the Drawable's dimensions
i.setLayoutParams(new Gallery.LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));
// Add the ImageView to the layout and set the layout as the content view
mLinearLayout.addView(i);
setContentView(mLinearLayout);
}
との使用について詳しくはCanvas
、Drawables
こちらをご覧ください。
于 2012-08-22T21:21:45.883 に答える