点击标记时弹出的信息窗口为空

9
这是我添加新标记到Google地图的代码:
MarkerOptions m = new MarkerOptions();
m.title(title);
m.snippet(snippet);
m.position(location);
mMap.addMarker(m);

mMap.setOnInfoWindowClickListener(new OnInfoWindowClickListener() {
        @Override
        public void onInfoWindowClick(Marker marker) {
            Toast.makeText(DemoActivity.this, "WindowClicked: "+ marker.getTitle() + ":" + marker.getSnippet(),
                    Toast.LENGTH_SHORT).show();
        }
    });

当我的标题(title)和摘要(snippet)为英文时,这个功能完美运作。信息窗口(infoWindow)和提示框(Toast)都按预期工作。

然而,在我的情况下,当titlesnippet为阿拉伯语时,Toast可以正常工作,但是信息窗口显示为空白

我认为这可能与阿拉伯语有关的一个bug。有没有方法来解决它?

3个回答

8

@jimmithy的答案解决了我的问题。

这只是从这个项目中实现它的三步:

第一步:创建信息窗口的布局。这里是popup.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="wrap_content"
android:orientation="horizontal">

<ImageView
android:id="@+id/icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:padding="2dip"
android:src="@drawable/ic_launcher"
android:contentDescription="@string/icon"/>

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

<TextView
android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="25sp"
android:textStyle="bold"/>

<TextView
android:id="@+id/snippet"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="15sp"/>
</LinearLayout>

</LinearLayout>

步骤2:创建您的InfoWindowAdapter实现。下面是PopupAdapter类:

class PopupAdapter implements InfoWindowAdapter {
  LayoutInflater inflater=null;

  PopupAdapter(LayoutInflater inflater) {
    this.inflater=inflater;
  }

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

  @Override
  public View getInfoContents(Marker marker) {
    View popup=inflater.inflate(R.layout.popup, null);

    TextView tv=(TextView)popup.findViewById(R.id.title);

    tv.setText(marker.getTitle());
    tv=(TextView)popup.findViewById(R.id.snippet);
    tv.setText(marker.getSnippet());

    return(popup);
  }
}

步骤三:在您的Activity中,将适配器设置为GoogleMap:

mMap.setInfoWindowAdapter(new PopupAdapter(getLayoutInflater()));

完成设置!


错误膨胀类,@此行 View popup=inflater.inflate(R.layout.popup, null);此外, 02-06 14:40:54.950: E/AndroidRuntime(24275): Caused by: android.content.res.Resources$NotFoundException: 资源不是可绘制项(颜色或路径):TypedValue{t=0x1/d=0x7f020078 a=-1 r=0x7f020078} - Abdul Wahab

7
有一个绕过这个问题的方法是显示自定义信息窗口。您可以通过创建 InfoWindowAdapter 并使用 GoogleMap.setInfoWindowAdapter()实现。

要替换默认信息窗口,请使用自定义渲染覆盖getInfoWindow(Marker)并在getInfoContents(Marker)中返回null。要仅替换默认信息窗口框架内的信息窗口内容(气泡),请在getInfoWindow(Marker)中返回null,并改写getInfoContents(Marker)。

更多信息: https://developers.google.com/maps/documentation/android/marker#info_windows


@iturki,这并没有解决我的问题。我在使用阿拉伯语时仍然有空白窗口。您到底是如何解决这个问题的? - AlAsiri
我想要将信息窗口文本居中对齐,因为在我的应用程序中,标记针文本是多语言的... 对于阿拉伯语,它会被左对齐,而一些文本则会隐藏在信息窗口中... - Abdul Wahab

3
你应该使用这个命令:.title("\u200e"+"عربی")

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