1
我創建了一個可以用來設置優先級的自定義彈出視圖。最初,當我嘗試設置顯示彈出窗口時,我用ImageView.pressed(true)
預設了ImageView
,顯示爲圖像的灰色背景。
現在,當我點擊另一張圖片時,我能夠註冊點擊但無法將更改顯示在屏幕上,即無法模擬按下的圖片上的ImageView.Pressed(true)
狀態。
我創建了一個可以用來設置優先級的自定義彈出視圖。最初,當我嘗試設置顯示彈出窗口時,我用ImageView.pressed(true)
預設了ImageView
,顯示爲圖像的灰色背景。
現在,當我點擊另一張圖片時,我能夠註冊點擊但無法將更改顯示在屏幕上,即無法模擬按下的圖片上的ImageView.Pressed(true)
狀態。
爲了達到同樣的目的,我使用了ImageButton
s,並且有一個方法來設置從onClick()
偵聽器調用的一組相關按鈕的狀態。例如,
private void configurePaletteStrokeWidth(Crayon.StrokeWidth strokeWidth) {
findViewById(R.id.iButtonStrokeWidthSmall).setSelected(false);
findViewById(R.id.iButtonStrokeWidthMedium).setSelected(false);
findViewById(R.id.iButtonStrokeWidthLarge).setSelected(false);
findViewById(R.id.iButtonStrokeWidthXLarge).setSelected(false);
switch (strokeWidth) {
case SMALL:
findViewById(R.id.iButtonStrokeWidthSmall).setSelected(true);
break;
case LARGE:
findViewById(R.id.iButtonStrokeWidthLarge).setSelected(true);
break;
case XLARGE:
findViewById(R.id.iButtonStrokeWidthXLarge).setSelected(true);
break;
case MEDIUM:
findViewById(R.id.iButtonStrokeWidthMedium).setSelected(true);
break;
}
}
每個按鈕的XML是類似於:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/button_stroke_width_small_tap" android:state_pressed="true"/> <!-- pressed -->
<item android:drawable="@drawable/button_stroke_width_small_tap" android:state_selected="true"/> <!-- selected -->
<item android:drawable="@drawable/button_stroke_width_small_normal" android:state_focused="true"/> <!-- focused -->
<item android:drawable="@drawable/button_stroke_width_small_normal"/> <!-- default -->
</selector>
感謝。但我的實現使用ImageButton/ImageViews動態添加到LinearLayout中。所以setPressed(ture/false)只能與ImageButton一起使用,就像ImageView一樣。我也添加drawables動態,所以選擇器不能實現,但我做的是我設置一個這樣的選擇器實現的背景。但即使使用ImageButton實現,也不能相應地設置按鈕。 – medampudi