是否可以在按钮顶部添加图像(视图)(作为背景图像)?
我正在将 iOS 应用程序移植到 Android 上,这在 iOS 上不是问题,但我想知道由于布局,这是否是 Android 上的正确方法。
编辑 :
为了澄清,请查看此屏幕截图:
我需要做左下角的“carte”按钮(法文卡)
我需要 :
- 带有背景图像的按钮
- 从互联网加载的按钮顶部显示的图像(一张卡片,有很多不同的,每天都会添加新闻卡片,在截图中它是“MIDI PASS”)
- 按钮上的文本本地化,所以我不能使用 Imagebutton 类。
是否可以在按钮顶部添加图像(视图)(作为背景图像)?
我正在将 iOS 应用程序移植到 Android 上,这在 iOS 上不是问题,但我想知道由于布局,这是否是 Android 上的正确方法。
编辑 :
为了澄清,请查看此屏幕截图:
我需要做左下角的“carte”按钮(法文卡)
我需要 :
目前尚不清楚您想要实现什么,但以下内容可能对您有所帮助:
ImageButtonandroid:background在 XML(使用)或代码(使用setBackgroundResource)中将图像设置为按钮的背景更新:
查看您更新的需求,最好使用自定义组件来实现您想要的。
您的问题有些不清楚,但据我了解,以下可能对您有用:
<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 变量中获取卡片图像的位图。
您还可以使用ImageView并实现 onClickListener。
是的,有可能。
然后使用 ImageButton....
设置你的 android:src="@drawable/foreground Image" 设置你的 android:background="@drawable/background Image"
因此,如果您想要一个苹果作为背景图像,一个 3-d 单词“苹果”作为您的前景图像。
你可以尝试这样的事情:
首先,为 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>
在这里,您可以定义 as 而android:drawable不仅仅是@drawable's,还可以定义@color's 或@layout's。如果你想要一个更复杂的布局,你应该定义一个按钮的背景图像和另一个图像在它上面使用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 文件中。