【问题标题】:Changing background image of button when clicked/focused doesn't work单击/聚焦时更改按钮的背景图像不起作用
【发布时间】:2016-07-07 14:40:33
【问题描述】:

我正在尝试使用此线程中的解决方案:

How to change background image of button when clicked/focused

但由于某种原因,没有任何效果。单击按钮时,没有任何变化。

这是activity_main.xml中的按钮:

<ImageButton
android:id="@+id/selectContactBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:contentDescription="contacts"
android:textSize="18sp"
android:layout_above="@+id/selectMediaBtn"
android:layout_alignRight="@+id/selectMediaBtn"
android:layout_alignEnd="@+id/selectMediaBtn"
android:background="@drawable/select_contact"/>

这是 select_contact.xml:

<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:drawable="@drawable/blocked_mc" />
<item android:drawable="@drawable/select_contact" />  //means normal
</selector>

这是按钮的 onClick():

Intent intent = new Intent(Intent.ACTION_PICK);
intent.setType(ContactsContract.CommonDataKinds.Phone.CONTENT_TYPE);
startActivityForResult(intent, ActivityRequestCodes.SELECT_CONTACT);

我做错了什么?

【问题讨论】:

    标签: android user-interface button background


    【解决方案1】:

    好的,我明白我的错误了。

    我将描述按下/未按下行为的可绘制对象下的 xml 命名为与可绘制图像本身相同的名称 (select_contact.xml)。

    当我将其更改为其他内容时,它起作用了。

    很傻,但已经很晚了,它发生了:)

    希望这可以帮助某人。

    【讨论】:

      猜你喜欢
      • 2011-12-09
      • 1970-01-01
      • 2017-03-07
      • 2013-04-25
      • 1970-01-01
      • 2011-12-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多