我正在尝试自定义 Google 地图中 InfoWindow 的 state_pressed 行为。通常,当按下此 InfoWindow 时,它会变为黄色。自定义 InfoWindows 也是如此。但我想把它改成另一种颜色,比如红色、橙色或蓝色。
所以我创建了一个非常简单的自定义信息窗口,如下所示:
class MyInfoWindowAdapter implements InfoWindowAdapter {
private final View myContentsView;
MyInfoWindowAdapter() {
myContentsView = getLayoutInflater().inflate(R.layout.popup, null);
}
@Override
public View getInfoContents(Marker marker) {
return null;
}
@Override
public View getInfoWindow(Marker marker) {
return getLayoutInflater().inflate(R.layout.popup2, null);
}
}
popup2 的 xml 只是添加了一个简单的可绘制对象,并在其中添加了一些文本,并使其可点击。可点击与否没有区别。
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:text="Test Popup"
android:background="@drawable/info_window"
android:clickable="true"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
</TextView>
最后是drawable:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="false">
<shape android:shape="rectangle">
<stroke android:width="1dp" android:color="@color/divider" />
<solid android:color="@color/background_button" />
<padding android:left="40dip"
android:top="10dip"
android:right="10dip"
android:bottom="40dp"/>
</shape>
</item>
<item android:state_pressed="true">
<shape android:shape="rectangle">
<stroke android:width="1dp" android:color="@color/divider" />
<solid android:color="@color/background_button_active" />
<padding android:left="40dip"
android:top="10dip"
android:right="10dip"
android:bottom="40dp"/>
</shape>
</item>
</selector>
这里的选择器是我希望改变 state_pressed 颜色的,但它没有。但是,如果我切换android.state_pressed
语句(第一个是真的,第二个是假的),那么选择器本身就会起作用,颜色确实会改变。但只有未按下状态发生变化,按下状态仍变为黄色。