How to add image for button in android?

前端 未结 9 1039
再見小時候
再見小時候 2021-02-02 06:44

How can I add an image to a button, rather than text?

相关标签:
9条回答
  • 2021-02-02 07:37

    you can use ImageView as Button. Create an ImageView and set clickable true after in write imageView.setOnClickListener for ImageView.

      <ImageView
    android:clickable="true"
    android:focusable="true"`
    android:id="@+id/imageview"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    />
    

    and in Activity's oncreate:

    imageView.setOnClickListener(...
    
    0 讨论(0)
  • 2021-02-02 07:39

    The new MaterialButton from the Material Components can include an icon:

    <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"/>
    

    You can also customize some icon properties like iconSize and iconGravity.

    Reference here.

    0 讨论(0)
  • 2021-02-02 07:42
       <Button
            android:id="@+id/button1"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="News Feed"
            android:icon="@drawable/newsfeed" />
    

    newsfeed is image in the drawable folder

    0 讨论(0)
提交回复
热议问题