我想添加一个ImageButton到我的布局,它具有缩小的效果,以便看起来像是被按下了。所以我基本上有原始图像@drawable/scan和按下的图像@drawable/scanhot。scanhot基本上是相同的图像缩小。
我的代码如下,但似乎不起作用。
如你所见,我使用的是Button而不是ImageButton,因为Button至少可以“切换”不同的图像。但是它对类似的图像(如scan/scanhot)没有任何作用
button_rescan.xml按钮

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

布局
<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="20dp"
    android:background="@android:color/transparent">

        <Button
            android:id="@+id/fragment_radar_rescan_btn"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@drawable/button_rescan"
            android:layout_gravity="center_horizontal"
            android:layout_centerHorizontal="true"/>
</RelativeLayout

最佳答案

你所做的问题是,当你将图片设置为背景时,它会自动放大以适应View。取而代之的是,这两张图片的分辨率应该相同,但Scanhot中的图片应该看起来更小。
换句话说,两张图片的分辨率都应该是132x29(或者说,它们的大小应该相同)

10-07 20:42