我在 iOS 中有以下实现并且运行良好。
private void States(UIButton button, UIImage unselectedImage, UIImage selectedImage)
{
button.SetImage (unselectedImage, UIControlState.Normal);
button.SetImage (selectedImage, UIControlState.Selected);
button.SetImage (selectedImage, UIControlState.Selected | UIControlState.Highlighted);
}
我正在尝试在 Android.Xamarin 中实现相同的功能,我实现了 ImageButton
而不是 Button
。我想知道如何更改 Android.Xamarin
中选中或未选中按钮的背景?
private void States(ImageButton button, string unselectedImage, string selectedImage)
{
// I could not able to change the background of the selected/unselected UmageButton
}
我遇到了以下站点 ( http://developer.xamarin.com/recipes/android/controls/imageview/display_an_image/ ),但是定义了两个属性 (button+imageView
) 来更改按钮背景图像。我认为 ImageButton 应该将所有这些功能集于一身。
下面的xml,只高亮一个,不高亮,我想一直高亮。
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:state_enabled="false"
android:drawable="@drawable/btn_view_off" />
<item
android:state_pressed="true"
android:state_enabled="true"
android:drawable="@drawable/btn_view_on" />
<item
android:state_focused="true"
android:state_enabled="true"
android:drawable="@drawable/btn_view_on" />
<item
android:state_enabled="true"
android:drawable="@drawable/btn_view_off" />
</selector>
最佳答案
您可以使用 selectors 执行此操作.
想法是为每个状态定义不同的drawable
:
<item
android:state_enabled="false"
android:drawable="@drawable/disabledbutton" />
<item
android:state_pressed="true"
android:state_enabled="true"
android:drawable="@drawable/pressedbutton" />
<item
android:state_focused="true"
android:state_enabled="true"
android:drawable="@drawable/focusedbutton" />
<item
android:state_enabled="true"
android:drawable="@drawable/normalbutton" />
关于android - 更改选定的 ImageButton 背景,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31484883/