在Google地图标记片段中对齐文本

3

我希望我的代码片段中的字符串能够居中对齐。

此外,代码片段中的换行符 (\n) 被转换为空格了,有没有方法可以插入换行符?

我的相关代码:

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

谢谢


您可以定义自己的InfoWindowAdapter。请查看Google Maps Demo,在那里您可以找到一个MarkerDemoActivity.java文件,他们在其中创建了一个CustomInfoWindowAdapter并将一个自定义的.xml文件分配给它。 - lambda
1个回答

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接