gpt4 book ai didi

具有选定状态的Android ImageButton?

转载 作者:IT老高 更新时间:2023-10-28 13:10:12 32 4
gpt4 key购买 nike

如果我使用带有选择器的 ImageButton 作为其背景,是否有我可以更改的状态来改变它的外观?现在我可以让它在按下时更改图像,但似乎没有“突出显示”或“选择”或类似状态让我随意切换它的外观。

这是我的 XML;它只会在按下时改变外观。

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

最佳答案

这对我有用:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- NOTE: order is important (the first matching state(s) is what is rendered) -->
<item
android:state_selected="true"
android:drawable="@drawable/info_icon_solid_with_shadow" />
<item
android:drawable="@drawable/info_icon_outline_with_shadow" />
</selector>

然后在java中:

//assign the image in code (or you can do this in your layout xml with the src attribute)
imageButton.setImageDrawable(getBaseContext().getResources().getDrawable(R.drawable....));

//set the click listener
imageButton.setOnClickListener(new OnClickListener() {

public void onClick(View button) {
//Set the button's appearance
button.setSelected(!button.isSelected());

if (button.isSelected()) {
//Handle selected state change
} else {
//Handle de-select state change
}

}

});

为了平滑过渡,您还可以提及动画时间:

<selector xmlns:android="http://schemas.android.com/apk/res/android" android:exitFadeDuration="@android:integer/config_mediumAnimTime">

关于具有选定状态的Android ImageButton?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2604599/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com