简体   繁体   English

如何在Android Studio中将图像放在按钮上

[英]How to put image on button in android studio

I have a button with xml background. 我有一个带有xml背景的按钮。 And I want to put image on this button. 我想将图像放在此按钮上。 It is not possible to put two backgrounds in one button. 不能将两个背景放在一个按钮中。 So, is there other way of putting image on my button while I have had a background yet? 那么,还有其他方法可以在我有背景时在按钮上放置图像吗? Below the code of my button: 在我的按钮代码下方:

<Button
            android:id="@+id/button4"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_column="0"
            android:layout_row="0"
            android:layout_gravity="fill"
            android:layout_rowWeight="1"
            android:layout_columnWeight="1"
            android:layout_margin="15dp"
            android:background="@drawable/readmore_back"
             />  

you may use 你可以用

<ImageButton
 ..
 ..
 ..
 android:src="@drawable/image1"
/>

instead of 代替

   <Button
    android:id="@+id/button4"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_column="0"
    android:layout_row="0"
    android:layout_gravity="fill"
    android:layout_rowWeight="1"
    android:layout_columnWeight="1"
    android:layout_margin="15dp"
    android:background="@drawable/readmore_back"
     />  

Or 要么

if you want to set the image to your existing Button then use 如果要将image设置为现有Button则使用

  • android:drawableTop , android:drawableTop
  • android:drawableBottom , android:drawableBottom
  • android:drawableLeft , android:drawableLeft
  • android:drawableRight

Use ImageButton instead of Button 使用ImageButton代替Button

<ImageButton
    android:src="@drawable/my_image"
    android:text="@string/IMAGE"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    />

You can try this: 您可以尝试以下方法:

<ImageView
            android:id="@+id/battery"
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:clickable="true"
            android:src="@drawable/laughing"/>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM