android - 更改选定的 ImageButton 背景

标签 android xamarin

我在 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/

相关文章:

android - 无法在 Visual Studio 2015 中创建新的空白应用 (Android)

android - 如何通过 xml 为自定义 View 传递重力属性?

android - 应用程序构建包后,启动屏幕和启动屏幕之间出现白屏index.html sencha touch

Xamarin:在后台更新 ObservableCollection

c# - 滚动时 Xamarin.Android 中的 ListView 内存不足

opencv - 如何在 Xamarin 中使用 openCV?

c# - xamarin应用程序SIGSEGV错误

android - 如何在谷歌地图 API V2 中获取从当前位置到纬度目的地的英里/公里和分钟

android - JSON 不适用于 Android Lollipop

android - 有没有等同于android提示的css?