テキストではなく画像をボタンに追加するにはどうすればよいですか?
9 に答える
むしろユーモラスに、タグを考慮して、ImageButtonウィジェットを使用するだけです。
ImageButton Viewを使用して、画像を設定するだけです:`
 <ImageButton
    android:id="@+id/searchImageButton"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_alignParentRight="true"
    android:src="@android:drawable/ic_menu_search" />
    彼が述べたように、ImageButtonウィジェットを使用しました。プロジェクトのディレクトリ内に画像ファイルをコピーしますRes/drawable/。XMLの場合、XMLファイルのグラフィック表現(簡単にするため)にImageButton移動し、追加したウィジェットをクリックし、そのプロパティシートに移動して、src:フィールドの[...]をクリックします。画像ファイルに移動するだけです。また、適切な形式を使用していることを確認してください。私は自分の理由で.pngファイルを使い続ける傾向がありますが、それらは機能します。
あなたはこのようなことを試みるべきです
    <Button
    android:id="@+id/imageButton1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@drawable/qrcode"/>
しandroid:background="@drawable/qrcode"ます
   <Button
        android:id="@+id/button1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="News Feed"
        android:icon="@drawable/newsfeed" />
ニュースフィードはドローアブルフォルダ内の画像です
マテリアルコンポーネントからの新しいMaterialButtonものには、アイコンを含めることができます。
<com.google.android.material.button.MaterialButton
    android:id="@+id/material_icon_button"
    style="@style/Widget.MaterialComponents.Button.TextButton.Icon"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/icon_button_label_enabled"
    app:icon="@drawable/icon_24px"/>
iconSizeやなどのアイコンプロパティをカスタマイズすることもできますiconGravity。
ここを参照してください。
画像をドローアブルフォルダに入れます。ここに私の画像ファイル名はleft.pngです
<Button
    android:id="@+id/button1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_x="118dp"
    android:layout_y="95dp"
    android:background="@drawable/left"
    android:onClick="toast"
    android:text=" " />
    Androidのactivity_main.xmlにImageButtonを作成できます。ボタンに配置する画像は、その画像を下の描画可能なフォルダーに貼り付けるだけです。これは、参照用のサンプルコードです。
<ImageButton
    android:id="@+id/button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:layout_marginBottom="49dp"
    android:layout_weight="1"
    android:onClick="prev"
    android:src="@drawable/prev"
    />
    ImageViewをボタンとして使用できます。ImageViewを作成し、ImageViewのimageView.setOnClickListenerを書き込んだ後、クリック可能をtrueに設定します。
  <ImageView
android:clickable="true"
android:focusable="true"`
android:id="@+id/imageview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
およびアクティビティのoncreate:
imageView.setOnClickListener(...