Как поставить рисование в качестве фона в InfoWindow (API Google Maps v2 для Android)?

Это мой собственный макет для моего информационного окна:

<RelativeLayout 
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/bg_infowindow" >
    <LinearLayout
            android:id="@+id/text_box"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="vertical" >
        <TextView 
            style="@style/TexTitle"
            android:id="@+id/title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
    <TextView 
            style="@style/TextDistance"
            android:id="@+id/distance"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
    </LinearLayout>
</RelativeLayout>

А это мой пользовательский адаптер:

public class MapInfoWindowAdapter implements InfoWindowAdapter{

    private LayoutInflater inflater;
    private Context context;

    public MapInfoWindowAdapter(Context context){
        inflater = (LayoutInflater) context.getSystemService( Context.LAYOUT_INFLATER_SERVICE );
        this.context = context;
    }

    @Override
    public View getInfoContents(Marker marker) {

        // Getting view from the layout file
        View v = inflater.inflate(R.layout.map_popup, null);

        TextView title = (TextView) v.findViewById(R.id.title);
        title.setText(marker.getTitle());

        TextView address = (TextView) v.findViewById(R.id.distance);
        address.setText(marker.getSnippet());

        return v;
    }

    @Override
    public View getInfoWindow(Marker arg0) {
        // TODO Auto-generated method stub
        return null;
    }

}

И вот результат:

введите здесь описание изображения

Однако я хочу, чтобы пользовательский рисунок был единственным фоном для моего информационного окна. Как этого добиться?


person IsaacCisneros    schedule 13.05.2013    source источник


Ответы (3)


Замените коды в getInfoContents на getInfoWindow. Разница между ними в том, что getInfoContents оборачивает View в ViewGroup с фоном по умолчанию.

@Override
public View getInfoWindow(Marker marker) {

    // Getting view from the layout file
    View v = inflater.inflate(R.layout.map_popup, null);

    TextView title = (TextView) v.findViewById(R.id.title);
    title.setText(marker.getTitle());

    TextView address = (TextView) v.findViewById(R.id.distance);
    address.setText(marker.getSnippet());

    return v;
}

@Override
public View getInfoContents(Marker arg0) {
    // TODO Auto-generated method stub
    return null;
}
person MaciejGórski    schedule 13.05.2013
comment
Я очень ценю ваш уточняющий ответ, однако я получал исключение NullPointerException, используя RelativeLayout в качестве корня в моем пользовательском макете, поэтому вместо этого я использую LinearLayout. - person IsaacCisneros; 13.05.2013
comment
@IsaacCisneros Тогда, вероятно, это должен быть ваш первоначальный вопрос. Это известная проблема. См. это: gmaps-api-issues . - person MaciejGórski; 13.05.2013
comment
@ MaciejGórski Я использовал тот же код. Он работает нормально. У меня возникли проблемы, когда я снова нажал на то же окно, я скрыл окно. Но в фоновом режиме отображается информационное окно по умолчанию. Вы удалили это? - person Parthi; 27.07.2015
comment
watsted 3 часа, пока я не нашел ваш ответ. Спасибо - person Khaled Hayek; 20.11.2018

попробуй это..

custom_infowindow.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">


<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#80000000" 
android:orientation="vertical">

<ImageView
    android:src="@drawable/ic_launcher"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" 
    android:padding="10dp"/>

</LinearLayout> 

</LinearLayout>


googleMap.setInfoWindowAdapter(new InfoWindowAdapter() 
{

        public View getInfoWindow(Marker arg0)
        {
            View v = getLayoutInflater().inflate(R.layout.custom_infowindow, null);
            return v;
        }

        public View getInfoContents(Marker arg0) 
        {
           return null;
        }
    });
person TheFlash    schedule 13.05.2013

в дополнение к принятому ответу я хочу отметить, что если вам все еще нужен пузырь с информационным окном, вы можете использовать эту библиотеку компоновки пузырьков

затем вы можете установить макет пузыря в качестве фона вашего маршрута в пользовательском макете, который вы надуваете.

person A.sobhdel    schedule 24.05.2019