レイアウトに画像を挿入するには Android 用アプリでどうすればいいですか?
Androidのレイアウトに画像を追加するにはいくつかの方法がありますので、一般的な方法を以下にご案内します。
- ImageView コントロールを使用する。ImageView コントロールをレイアウトファイルに追加し、src 属性を画像のリソース ID または画像の URL アドレスに設定する。
<ImageView
android:id="@+id/imageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/image" />
- レイアウトのbackground属性に画像リソースIDや画像URLを設定する。
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/image">
<!-- 布局内容 -->
</LinearLayout>
- ImageButtonコントロールを使用するには、レイアウトファイルにImageButtonコントロールを追加し、src属性に画像のリソースIDまたは画像のURLアドレスを設定します。
<ImageButton
android:id="@+id/imageButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/image" />
- BitmapDrawableで画像を設定する場合は、BitmapDrawableクラスをコードで利用して画像を設定し、レイアウトの背景やImageViewコントロールのsrc属性に設定します。
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.image);
BitmapDrawable bitmapDrawable = new BitmapDrawable(getResources(), bitmap);
imageView.setImageDrawable(bitmapDrawable);
画像を追加する一般的な方法は上記のとおりです。ニーズに合わせて適切な方法を選択してください。