How to customise the state_pressed colour of an infowindow in Google Maps?

流过昼夜 提交于 2020-01-14 10:44:07

问题


I am trying to customise the state_pressed behaviour of an InfoWindow in Google Maps. Normally when this InfoWindow is pressed, it turns yellow. That's also the case with custom InfoWindows. But I want to change this to another colour, like red or orange or blue.

So I created a very simple custom InfoWindow like this:

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);
    }
}

The xml for popup2 just adds a simple drawable and adds a little bit of text to it, and made clickable. Clickable or not does not make a difference.

<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>

And finally the 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>

The selector here is what I hoped would make the state_pressed colour to change, but it doesn't. The selector itself is acted upon though, if I switch the android.state_pressed statements (first true, second false) the colour does change. But only the not pressed state changes, the pressed state still gets the yellow colour.


回答1:


In simple words "YOU CANT" .

because as per documentation "An info window is not a live View, rather the view is rendered as an image onto the map. As a result, any listeners you set on the view are disregarded and you cannot distinguish between click events on various parts of the view. You are advised not to place interactive components — such as buttons, checkboxes, or text inputs — within your custom info window."

So, in short u can't handle any events on infowindow other than "OnInfoWindowClickListener"

For more check "https://developers.google.com/maps/documentation/android/marker".




回答2:


You may want to upvote this enhancement here: http://code.google.com/p/gmaps-api-issues/issues/detail?id=4783



来源:https://stackoverflow.com/questions/15531578/how-to-customise-the-state-pressed-colour-of-an-infowindow-in-google-maps

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!