【问题标题】:setting onclick image for image button in android在android中为图像按钮设置onclick图像
【发布时间】:2013-07-23 06:09:58
【问题描述】:

我有一个图像按钮,我使用 android:src 标签在我的 XML 中加载了一个图像。 我有几个基于图像按钮的问题:

  1. 我的图像已调整为宽高比 130:51,但是当我将图像按钮的宽度和高度设置为 wrap_content 时,我看到图像被加载为裁剪,我希望它完全适合图像按钮,以便间距应该不可见。

  2. 当我运行应用程序并点击图像按钮时,我想要一种状态变化的感觉。但目前我看到它看起来像一个静态图像,当我点击它时,它并没有显示推送状态的变化。为了实现,我想在用户点击事件时加载另一张图片,这样他就可以看到状态变化并感觉到按钮被点击了。

请帮帮我。

【问题讨论】:

    标签: android imagebutton android-button


    【解决方案1】:

    试试这个:

    <?xml version="1.0" encoding="utf-8"?>
    <selector xmlns:android="http://schemas.android.com/apk/res/android">
        <item android:drawable="@drawable/button_pressed_yellow"
              android:state_pressed="true" />
        <item android:drawable="@drawable/button_focused_orange"
              android:state_focused="true" />
        <item android:drawable="@drawable/button_normal_green" />
    </selector>
    

    将此 XML 保存为您想要的任何名称。

    然后用这个 XML 设置按钮的背景。所以这将根据给定的状态更改图像。

    【讨论】:

    • 请告诉我如何设置图像的比例因子,使其完全适合图像按钮。
    • 在图像视图xml中使用android:scaletype
    • 你可以像@shreyas 所说的那样做。你也可以以编程方式进行 image.setScaleType(ImageView.ScaleType.MATRIX);
    • 我尝试了 scaletype = fitXY, center 等,但它们都没有像我想要的那样正常工作
    • 尝试使用按钮而不是图像按钮。
    【解决方案2】:

    1) my_image.setScaleType(ScaleType.FIT_XY);

    2)

    <selector xmlns:android="http://schemas.android.com/apk/res/android">
            <item android:state_pressed="true"
                android:drawable="@drawable/btn_cab_done_pressed_example" />
            <item android:state_focused="true" android:state_enabled="true"
                android:drawable="@drawable/btn_cab_done_focused_example" />
            <item android:state_enabled="true"
                android:drawable="@drawable/btn_cab_done_default_example" />
        </selector>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-08-13
      • 2011-02-15
      • 1970-01-01
      • 2014-03-01
      • 1970-01-01
      相关资源
      最近更新 更多