【问题标题】:image on top of button android按钮android顶部的图像
【发布时间】:2012-06-26 00:11:28
【问题描述】:

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

我正在将 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”按钮(法文卡)

我需要:

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

【问题讨论】:

  • 如果我理解正确,您希望图像充当按钮
  • 无法使用 imagebutton 吗?

标签: android android-layout button imageview


【解决方案1】:

不太清楚你想要达到什么目标,但以下内容可能对你有所帮助:

更新

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

【讨论】:

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

    【讨论】:

    • 看这个截图:a4.mzstatic.com/us/r1000/062/Purple/v4/7c/4b/cd/…,左下角的按钮就是我需要的。这是一个按钮,只在某些情况下显示,表示需要一张卡片,并且有很多不同的卡片,所以我不能使用图像资源。我不得不在代码中更改卡片图像。
    • 背景是否对所有人都通用?
    【解决方案3】:

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

    【讨论】:

      【解决方案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 文件中。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2011-03-18
          • 1970-01-01
          • 2016-07-04
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2011-11-12
          相关资源
          最近更新 更多