【问题标题】:image on top of button android按钮android顶部的图像
【发布时间】:2012-06-26 00:11:28
【问题描述】:
【问题讨论】:
标签:
android
android-layout
button
imageview
【解决方案2】:
您的问题有些不清楚,但据我了解,以下可能对您有用:
<ImageButton android:id="@+id/imgButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/bg_image"
android:src="@drawable/top_image"/>
希望对您有所帮助。
更新:
如果您的背景是普通的,那么您可以使用以下代码设置位图:
((ImageButton)findViewById(R.id.imgButton)).setImageBitmap(bmp);
在这里,您需要在 bmp 变量中获取卡片图像的位图。
【解决方案4】:
是的,这是可能的。
然后使用 ImageButton....
设置你的 android:src="@drawable/foreground Image"
设置你的 android:background="@drawable/background Image"
因此,如果您想要一个苹果作为背景图像,一个 3-d 单词“苹果”作为您的前景图像。
【解决方案5】:
你可以试试这样的:
首先,在 res/drawable/ 文件夹中为按钮创建一个选择器(我们称之为 selector_button.xml):
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:drawable="@drawable/image_resource_for_button_pressed"
android:state_pressed="true" />
<item android:drawable="@drawable/image_resource_for_button_pressed"
android:state_focused="true" />
<item android:drawable="@drawable/image_resource_for_button_normal" />
</selector>
在这里,您可以定义为和android:drawable,而不仅仅是@drawable,还可以定义@color 或@layout。如果您想要一个更复杂的布局,您应该使用按钮的背景图像定义一个,并在其顶部使用 RelativeLayout 定义另一个图像。
为此,您的 res/drawable/ 文件夹中必须有 image_resource_for_button_pressed.png(用于按下和聚焦状态)和 image_resource_for_button_normal.png(用于正常状态)。
之后,您创建一个按钮,如下所示:
<Button
android:id="@+id/aButton"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@drawable/selector_button"
android:text="Hardcoded string" />
这种方法可以帮助您保持代码的可读性,因为您只是将图像资源的更改提取到一个 .xml 文件中。