【问题标题】:Button pressed and two images xml android按下按钮和两个图像 xml android
【发布时间】:2017-01-23 13:42:50
【问题描述】:

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

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

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

<?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>

this are the two images

this is the goal

我也在尝试以这种方式使用 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>

【问题讨论】:

  • 请提供图片。
  • @AbhriyaRoy 抱歉,现在是的

标签: android xml android-layout button


【解决方案1】:

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

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

【讨论】:

  • 我知道了,谢谢,但是我需要在按下时显示两张图像,当按下时显示一张,我将图像与我需要遵循的模型一起上传。
  • 按下按钮时你想如何显示两个图像?
  • 您需要使用图层列表再创建一个可绘制对象。然后它会工作。
  • 我不知道是否可行,但这是我的目标:i.stack.imgur.com/ZI1oa.png
【解决方案2】:

您可以为此使用相对布局

<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 的图像 祝你好运

【讨论】:

  • gosavi 谢谢,但我需要像按钮一样使用它,当用户按下时必须更改当前图像。
  • 你可以用这段代码来做,只需改变你想要的 android:id="@+id/iv_img" 图像,并将选择器设置为这个图像和 android:id="@+id /iv_background" 是阴影对所有人保持不变
【解决方案3】:

请使用此代码更改可绘制选择器文件..

<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..

如果需要更多解释或帮助,请告诉..

【讨论】:

  • 谢谢!这做我想要的! ,只是还是一个小问题,当我按下按钮并显示两个图像时,数字变得比以前大,可以保持“number_1”与相同的尺寸吗?
  • 根据需要将此行的边距更改为更多...
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-07-03
  • 2010-12-04
  • 2011-06-12
相关资源
最近更新 更多