开发者

android: imageview inside button

开发者 https://www.devze.com 2023-02-17 00:01 出处:网络
I need to be able to show a button in my layout, but I want the button to have an image (small Icon) and text inside of it.

I need to be able to show a button in my layout, but I want the button to have an image (small Icon) and text inside of it.

something like this:
  ___________________
 / ***   SOME TEXT  /
/_**_______________/

where the *s are an image

So what I thought to do is开发者_运维百科 a linear layout with a background and make it clickable - which will probably work, but that means that it won't behave exactly like a button...

Do you have a better idea on how to do that?

Thanks, e.


In your xml layout for the button, add:

<Button
  ...
  android:drawableLeft="@drawable/myIcon"
  android:drawablePadding="5dp"
  android:paddingLeft="10dp"
  android:text="My text" />

Where myIcon.png is your icon in the drawable folder.

EDIT Added padding. EDIT 2: Added padding to left of the icon.


In your XML, you can use ImageButton (which was added in API level 1, so it should be available)

<ImageButton
   android:id="@+id/imageBtn"
   android:layout_gravity="center"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:src="@drawable/btnSrc"
     />

This method doesn't require you to add padding to a side.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号