【问题标题】:How to put drawable as a background on InfoWindow (Google Maps API v2 for Android)?如何将可绘制对象作为 InfoWindow(Android 版 Google Maps API v2)的背景?
【发布时间】:2013-05-13 09:05:44
【问题描述】:

这是我的信息窗口的自定义布局:

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

}

结果如下:

但是我希望自定义可绘制对象作为我的信息窗口的唯一背景,如何实现这一点?

【问题讨论】:

    标签: android google-maps-android-api-2


    【解决方案1】:

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

    【讨论】:

    • 我非常感谢您的澄清回答,但是我在自定义布局中使用 RelativeLayout 作为根得到了 NullPointerException,所以我改用 LinearLayout。
    • @IsaacCisneros 那应该是你最初的问题。这是一个已知的问题。看到这个:gmaps-api-issues.
    • @MaciejGórski 我使用了相同的代码。它工作正常。在我隐藏窗口的同一个窗口上再次单击时我遇到了麻烦。但在后台显示的默认信息窗口仍然存在。你删除了吗?
    • 花了 3 个小时才找到您的答案。谢谢
    【解决方案2】:

    试试这个..

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

    【讨论】:

      【解决方案3】:

      除了已接受的答案,我想提一下,如果您仍然想要信息窗口气泡,您可以使用this bubble layout library

      然后您可以在您正在充气的自定义布局中将气泡布局设置为您的路线视图的背景。

      【讨论】:

        猜你喜欢
        • 2014-10-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-12-13
        • 1970-01-01
        • 2012-12-05
        • 1970-01-01
        相关资源
        最近更新 更多