Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/204.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 按下时的图像按钮状态不显示_Android_Selector_Imagebutton - Fatal编程技术网

Android 按下时的图像按钮状态不显示

Android 按下时的图像按钮状态不显示,android,selector,imagebutton,Android,Selector,Imagebutton,我试图创建一个按钮,它有一个默认的图像,但当按下它切换到一个新的图像。我已经用过了,但不起作用 这是我的info\u按钮\u选择器。xml: <?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:state_pressed="true" androi

我试图创建一个按钮,它有一个默认的图像,但当按下它切换到一个新的图像。我已经用过了,但不起作用

这是我的
info\u按钮\u选择器。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>
ic\u菜单\u info\u details2
图像始终显示在屏幕上,这是预期的。这表明按钮使用XML文件作为其可绘制资源,但为什么按下按钮时不会更改其图像

编辑

使用Joss下面的答案,图像仍然没有改变,现在以一种奇怪的方式拉伸。注:两幅图像完全相同,但第二幅图像的缩放比例为尺寸的60%。这可能意味着它正在工作,但由于视图被拉伸,我无法判断

                <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" />

我使用了这两个版本的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>

设置可拖动到图像按钮背景而不是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" />

编辑以跟踪失真问题:

你能做的就是把这些层分开

因为背景是可拉伸的,所以将其设置为背景,分离不应拉伸的图像,并将其设置为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" />

这样,您可以将背景作为渐变,并在不降低质量的情况下进行拉伸,并且SRC图像不会调整大小和扭曲

这就是ImageButton的真正用途


或者,按照我在第一个答案中的建议做,但使用按钮代替。让我知道这是否有帮助。

您应该尝试使用已完成的不同图像获取详细信息2,并查看图像是否确实在更改,但没有重新调整大小。我不希望有状态按钮会重新调整大小,可能会使用两个不同的图像并设置可见性。

谢谢,我尝试了一下,但没有解决问题。如果你有时间,我会更新我的问题。编辑我的答案以添加更多描述。是的,我刚刚这么做了,它工作正常,图像现在只是奇怪地拉伸。
 <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" />
<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" />