简体   繁体   English

按住时可绘制按钮更改

[英]button change drawable on holding

I'm trying to make this volume button that changes the drawable according to the state that it is put in. So on default the Volume of the device will be on, when pressed it will mute the device, and when its held it'll change the device to vibrating only. 我正在尝试使此音量按钮根据可绘制状态改变其绘制状态。因此,默认情况下,设备的音量将打开,按下该按钮将使该设备静音,并且在按住它时会将设备更改为仅振动。 Now I am trying to change the drawable of when its put on vibrating, but nothing I try seems to work. 现在,我试图更改可绘制对象的振动状态,但似乎没有任何效果。 I currently have this code in my XML, and I tried playing around with the pressed state but I couldn't get something to work that changes the drawable accordingly. 我目前在XML中有此代码,我尝试在按下状态下进行操作,但无法进行相应的工作来更改可绘制对象。

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

    <item android:drawable="@drawable/ic_volume_down_black_24dp"
    android:state_checked="true" />

    <item android:drawable="@drawable/ic_volume_off_black_24dp"
    android:state_checked="false"/>    
</selector>

Add this android:state_pressed . 添加此android:state_pressed

<item android:drawable="@drawable/image_you_want_on_press_state"
android:state_pressed="true" />`

Hope this Help. 希望这个帮助。

I created a new drawable xml file, for eg, image_state.xml 我创建了一个新的可绘制xml文件,例如image_state.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true">
    <bitmap android:src="@drawable/image_pressed"/>
</item>
<item>
    <bitmap android:src="@drawable/image"/>
</item>
</selector>

And in my layout file, I set the src of the imageView as: 在我的布局文件中,将imageView的src设置为:

<ImageView
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:src="@drawable/image_state"/>

Also check this : Change source image for image view when pressed 还要检查一下: 按下时将源图像更改为图像视图

Default drawable <item android:drawable="@drawable/ic_volume_off_black_24dp"></item> and for the pressed st <item android:drawable="@drawable/ic_volume_down_black_24dp" android:state_pressed="true" /> 默认的drawable <item android:drawable="@drawable/ic_volume_off_black_24dp"></item> ,对于按下的st <item android:drawable="@drawable/ic_volume_down_black_24dp" android:state_pressed="true" />

<selector xmlns:android="http://schemas.android.com/apk/res/android" android:exitFadeDuration="200">
    <item android:drawable="@drawable/ic_volume_down_black_24dp" android:state_activated="true" />
    <item android:drawable="@drawable/ic_volume_down_black_24dp" android:state_pressed="true" />
    <item android:drawable="@drawable/ic_volume_off_black_24dp"></item>
</selector>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM