2013-04-03 22 views
3

我希望我的代码段中的字符串与中心对齐。对齐谷歌地图标记片段中的文字

此外,代码段中的断路器(\ n)被转换为空格,是否有插入断路器的方法?

我的相关代码:

GoogleMap map = ... 
map.addMarker(new MarkerOptions().position(pos) 
     .title("title") 
     .snippet("body \n and mind"); 

感谢

+0

您可以定义自己的[InfoWindowAdapter](https://developers.google.com/maps/documentation/android/reference/com/google/android/gms/maps/GoogleMap.InfoWindowAdapter)。查看[Google Maps Demo](https://developers.google.com/maps/documentation/android/intro#sample_code),您可以在其中找到一个MarkerDemoActivity.java,在这里他们创建一个CustomInfoWindowAdapter并将一个自定义的.xml文件分配给它。 – lambda

回答

12

使用一些例子,以及从答案:custom info window adapter with custom data in map v2,我遇到了以下解决方案:

marker.xml

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

    <TextView 
    android:id="@+id/info" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:gravity="center" /> 

</RelativeLayout> 

Java:

map.setInfoWindowAdapter(new InfoWindowAdapter() { 

     @Override 
     public View getInfoWindow(Marker arg0) { 
      return null; 
     } 

     @Override 
     public View getInfoContents(Marker marker) { 

      View v = getLayoutInflater().inflate(R.layout.marker, null); 

      TextView info= (TextView) v.findViewById(R.id.info); 

      info.setText(marker.getPosition().toString()); 

      return v; 
     } 
    });