简体   繁体   English

按下时ImageButton的状态不显示

[英]ImageButton state when pressed not showing up

I am trying to create a button that has a default image, but when it is pressed it switches to a new image. 我正在尝试创建一个具有默认图像的按钮,但是当它被按下时它将切换到一个新图像。 I have used this tutorial along with this question but it will not work. 我已将this tutorialthis question一起使用,但无法正常工作。

This is my info_button_selector.xml: 这是我的info_button_selector.xml:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true"
          android:drawable="@drawable/ic_menu_info_details2" /> <!-- pressed -->
    <item android:state_focused="true"
          android:drawable="@drawable/ic_menu_info_details2" /> <!-- focused -->
    <item android:drawable="@drawable/ic_menu_info_details" /> <!-- default -->
</selector>

This is the code with my ImageButton: 这是我的ImageButton的代码:

                <ImageButton
                    android:id="@+id/apModeInfoButton"
                    android:layout_width="0dip"
                    android:background="@null"
                    android:layout_height="match_parent"
                    android:layout_weight="1.15"
                    android:clickable="true"
                    android:src="@drawable/info_button_selector" />

The button stays at the initial state looking like ic_menu_info_details the entire time. 该按钮保持初始状态,就像整个时间中ic_menu_info_details一样。

I changed my info_button_selector.xml file to this: 我将info_button_selector.xml文件更改为此:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true"
          android:drawable="@drawable/ic_menu_info_details2" /> <!-- pressed -->
    <item android:state_focused="true"
          android:drawable="@drawable/ic_menu_info_details2" /> <!-- focused -->
    <item android:drawable="@drawable/ic_menu_info_details2" /> <!-- default -->
</selector>

The ic_menu_info_details2 image was the displayed the entire time, which is expected. ic_menu_info_details2图像一直显示,这是预期的。 This shows that the button IS using the XML file as its drawable resource, but why is it that the button does not change its image when pressed? 这表明按钮使用XML文件作为其可绘制资源,但是为什么按钮在按下时不改变其图像呢?

EDIT 编辑

Using Joss's answer below, the image still does not change, and is now stretched in a strange way. 使用下面的乔斯答案,图像仍然没有改变,并且现在以一种奇怪的方式被拉伸了。 Note: Both images are exactly the same except the second image is scaled to be 60% the size. 注意:除第二张图像缩放为60%的大小外,两张图像完全相同。 This might mean that it is working but I cannot tell as the view is stretched. 这可能意味着它正在工作,但是由于视图被拉伸,我无法确定。

                <ImageButton
                    android:id="@+id/apModeInfoButton"
                    android:layout_width="0dip"
                    android:layout_height="match_parent"
                    android:layout_weight="1.15"
                    android:clickable="true"
                    android:background="@drawable/info_button_selector" />

I used both of these versions of the XML file as well as different combinations of the images, all of the combinations resulted in the same sized image never changing. 我使用了这两个版本的XML文件以及图像的不同组合,所有这些组合导致尺寸相同的图像永不变。

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true"
          android:drawable="@drawable/ic_menu_info_details" /> <!-- pressed -->
    <item android:state_focused="true"
          android:drawable="@drawable/ic_menu_info_details" /> <!-- focused -->
    <item android:drawable="@drawable/ic_menu_info_details" /> <!-- default -->
</selector>

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true"
          android:drawable="@drawable/ic_menu_info_details2" /> <!-- pressed -->
    <item android:state_focused="true"
          android:drawable="@drawable/ic_menu_info_details2" /> <!-- focused -->
    <item android:drawable="@drawable/ic_menu_info_details2" /> <!-- default -->
</selector>

Set that drawable to the ImageButton background rather than the src . 将可绘制对象设置为ImageButton 背景,而不是src

 <ImageButton
          android:id="@+id/apModeInfoButton"
          android:layout_width="0dip"
          android:background="@drawable/info_button_selector"
          android:layout_height="match_parent"
          android:layout_weight="1.15"
          android:clickable="true" />

Edit to follow up the distortion problem: 编辑以解决变形问题:

Well what you can do is to have the layers separate. 那么,您可以做的是将各层分开。

Because the background is stretchable, set this to the BACKGROUND and separate the image which shouldn't stretch and set this to the SRC. 由于背景是可拉伸的,因此请将其设置为“背景”,然后将不应拉伸的图像分开,并将其设置为SRC。

<ImageButton
         android:id="@+id/apModeInfoButton"
         android:layout_width="0dip"
         android:src="@drawable/non_scaleable_image"
         android:background="@drawable/gradient_bg"
         android:layout_height="match_parent"
         android:layout_weight="1.15"
         android:clickable="true" />

This way, you could have a background as a gradient and have it stretch without loosing quality, and the SRC image would not resize and distort. 这样,您可以将背景作为渐变,并且可以拉伸而不会降低质量,并且SRC图像不会调整大小和变形。

That is really what an ImageButton should be used for. 这确实是ImageButton应该用于的目的。

Alternatively, do what I suggested in the first answer, but use a Button instead. 或者,执行我在第一个答案中建议的操作,但改用Button。 Let me know if this helps. 让我知道是否有帮助。

You should try using a completed different image for details2 and see if the image is in fact changing, but not being re-sized. 您应尝试使用完整的其他图像作为details2,并查看图像是否实际上在更改,但未调整大小。 I wouldn't expect a stateful button to re-size, maybe use two different images and set visibility. 我不希望有状态按钮会重新调整大小,也许不会使用两个不同的图像并设置可见性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM