按钮android顶部的图像

时间:2012-06-25 13:05:55

标签: android android-layout button imageview

是否可以在按钮上添加图像(视图)(作为背景图像)?

我正在将一个iOS应用程序移植到Android上,这在iOS上不是问题,但我想知道这是否是因为布局而在Android上的正确方法。

编辑:

要澄清,请检查此屏幕截图:

http://a4.mzstatic.com/us/r1000/062/Purple/v4/7c/4b/cd/7c4bcd53-ba55-94d7-a26c-ce1bfe040003/mza_2736801523527387264.320x480-75.jpg

我需要左下角按钮“carte”(法语卡片)

我需要:

  • 带有背景图片的按钮
  • 显示在从互联网上加载的按钮顶部的图像(一张卡,每天都会添加很多不同的新闻卡,在screnshot中它是“MIDI PASS”)
  • 一个本地化在按钮上的文本,所以我不能使用Imagebutton类。

5 个答案:

答案 0 :(得分:2)

目前尚不清楚您想要达到的目标,但以下内容对您有所帮助:

<强>更新

查看更新后的要求,最好使用custom component来实现您的目标。

答案 1 :(得分:1)

你的问题有点不清楚,但从我的理解,以下可能对你有用:

        <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变量中获取卡片图像的位图。

答案 2 :(得分:0)

您还可以使用ImageView并实现onClickListener。

答案 3 :(得分:0)

是的,这是可能的。

然后使用ImageButton ....

设置你的android:src =“@ drawable / foreground Image”  设置你的android:background =“@ drawable / background Image”

因此,如果您想为背景图像添加一个苹果,并为前景图像添加一个三维单词“apple”。

答案 4 :(得分:0)

您可以尝试这样的事情:

首先,为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文件中。

相关问题