Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/213.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_Xml_Android Layout_Button - Fatal编程技术网

Android 按下按钮,两幅图像显示在屏幕上

Android 按下按钮,两幅图像显示在屏幕上,android,xml,android-layout,button,Android,Xml,Android Layout,Button,我有两张照片。一个是数字,另一个是阴影。我需要展示这两张图片,如第二张照片所示 这是我的密码。现在它只显示数字(未按下时)或阴影(按下时) 当用户按下按钮时,如何显示两个图像 <?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:drawable="@drawable

我有两张照片。一个是数字,另一个是阴影。我需要展示这两张图片,如第二张照片所示

这是我的密码。现在它只显示数字(未按下时)或阴影(按下时)

当用户按下按钮时,如何显示两个图像

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:drawable="@drawable/button_select" android:state_pressed="true"
        android:alpha="0.75"/>
    <item android:drawable="@drawable/number_1" android:state_enabled="true" />

    <item android:drawable="@drawable/number_1" android:state_pressed="false"
        />
</selector>

此外,我尝试以这种方式使用layerDrawable,但没有成功:

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:drawable="@drawable/button_select" android:state_pressed="true"
        android:alpha="0.75"/>
    <item android:drawable="@drawable/number_1" android:state_enabled="true" />
</selector>

</item>
    <selector xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:drawable="@drawable/number_1" android:state_pressed="false"
        />
</selector>
</layer-list>

您应该使用两个不同的图像

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/pressed_image" android:state_pressed="true"/>
    <item android:drawable="@drawable/normal_image" android:state_pressed="false"/>
</selector>

您可以使用相对布局

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" >

    <ImageView
        android:id="@+id/iv_background"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/button_select"/>
    <ImageView
        android:id="@+id/iv_img"
        android:layout_width="wrap_content"
        android:padding="5dp"
        android:layout_height="wrap_content"
        android:background="@drawable/number_1"/>

</RelativeLayout> 


只需更改iv_img id的图像,祝您好运,请使用此代码更改可绘制的选择器文件

<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true">
    <layer-list>
        <item android:drawable="@drawable/number_1" />
        <item android:bottom="@dimen/margin_5" android:left="@dimen/margin_5" android:right="@dimen/margin_5" android:top="@dimen/margin_5">
            <bitmap android:src="@drawable/button_select" />
        </item>
    </layer-list>
</item>
<item android:drawable="@drawable/number_1" android:state_pressed="false"/>

限制::

它可以用于数字的单个图像。对于多个数字,您必须为每个数字创建不同的selector.xml


如果需要更多解释或帮助,请告知。

请提供图像。@AbhriyaRoy抱歉,现在我知道了,谢谢,但按下时我需要显示两个图像,按下时需要显示一个图像,我上传了我需要遵循的模型的图像。当按下按钮时,您希望如何显示两个图像?您需要使用图层列表创建一个可绘制的图像。我不知道是否可能,但这是我的目标:参考这个链接。。。gosavi谢谢,但我需要像按钮一样使用它,当用户按下时必须更改当前图像。您可以使用此代码执行此操作,只需更改android:id=“@+id/iv_img”图像任何您想要的内容,并将选择器设置为可绘制此图像,android:id=“@+id/iv_background”为阴影,保持不变,谢谢!这就是我想要的,仍然是一个小问题,当我按下按钮并显示两幅图像时,数字会比以前大,是否可以保持“数字1”的相同尺寸。。。