在 Google Maps api v2 中设置自定义 InfoWindow 的宽度

Posted

技术标签:

【中文标题】在 Google Maps api v2 中设置自定义 InfoWindow 的宽度【英文标题】:Set width of custom InfoWindow in Google Maps api v2 【发布时间】:2014-05-28 15:18:28 【问题描述】:

我在地图上创建了标记。当我点击它们时,自定义信息窗口总是和屏幕一样宽。

我尝试设置 layout_width="200dp" 但这没有帮助。我也试过设置

view.setLayoutParams(new LayoutParams(GetDipsFromPixel(200), GetDipsFromPixel(300)));

我可以通过设置snippetUi.setWidth(GetDipsFromPixel(200)); 来设置文本视图的宽度,但布局仍然保持屏幕宽度。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_
    android:layout_
    android:orientation="vertical"
    android:background="#FFFFFF"
    android:layout_marginLeft="30dp"
    android:layout_marginRight="30dp"
    android:padding="10dp" >

    <LinearLayout
        android:layout_
        android:layout_
        android:orientation="horizontal"
        android:background="#FFFFFF"
        android:gravity="center_vertical">

        <ImageView
            android:id="@+id/worldmap_infowindow_profileimg"
            android:layout_
            android:layout_
            android:src="@drawable/noimage" />

        <TextView
            android:id="@+id/worldmap_infowindow_username"
            android:layout_
            android:layout_
            android:text="To do No.2."
            android:textColor="#000000"
            android:textSize="16sp"
            android:textStyle="bold" 
            android:paddingLeft="5dp"/>
    </LinearLayout>

    <View android:id="@+id/separator" 
         android:background="#ababab" 
         android:layout_width = "fill_parent"
         android:layout_marginTop="3dp"
         android:layout_marginBottom="3dp"
         android:layout_/>

    <TextView
        android:id="@+id/worldmap_infowindow_name"
        android:layout_
        android:layout_
        android:text="See the Giza Pyramids"
        android:textColor="#000000"
        android:textSize="16sp"
        android:textStyle="bold" />

    <TextView
        android:id="@+id/worldmap_infowindow_details"
        android:layout_
        android:layout_
        android:text="It was fascinating! It's a shame that it was raining, though."
        android:textColor="#000000"
        android:textSize="14sp"
        android:textStyle="normal" />

</LinearLayout>

类:

 private class CustomInfoWindowAdapter implements InfoWindowAdapter 

       private View view;

       public CustomInfoWindowAdapter() 
            view = getLayoutInflater().inflate(R.layout.custom_infowindow, null);
       

       @Override
       public View getInfoContents(Marker marker) 

           if (WorldMap.this.myMarker != null
                   && WorldMap.this.myMarker.isInfoWindowShown()) 
               WorldMap.this.myMarker.hideInfoWindow();
               WorldMap.this.myMarker.showInfoWindow();
           
           return null;
       

       @Override
       public View getInfoWindow(final Marker marker) 
           WorldMap.this.myMarker = marker;


           final String title = marker.getTitle();
           final TextView titleUi = ((TextView) view.findViewById(R.id.worldmap_infowindow_name));
           if (title != null) 
               titleUi.setText(title);
            else 
               titleUi.setText("");
           

           final String snippet = marker.getSnippet();
           final TextView snippetUi = ((TextView) view.findViewById(R.id.worldmap_infowindow_details));
           if (snippet != null) 
               snippetUi.setText(snippet);
            else 
               snippetUi.setText("");
           

           return view;
       
   

【问题讨论】:

希望这会帮助你***.com/questions/16711522/…@erdomester 确实有帮助,谢谢。我刚刚添加了android:background="@drawable/bg_custominfowindow",其中 bg_c​​ustominfowindow 是在 xml 中创建的形状。请将此添加为解决方案。 非常有帮助..我将用简短的解释添加答案。 使用 android:minWidth 参数进行自定义布局.. 【参考方案1】:

基本上你只需要在根目录下有另一层布局,像这样:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_
android:background="@android:color/transparent"
android:layout_>

<LinearLayout
    android:layout_
    android:layout_
    android:background="@android:color/white"
    android:orientation="horizontal">
    ...

【讨论】:

如果有人希望这个解决方案在不同维度的屏幕上工作,那么只需从 diiferent dimen.xml 中获取值【参考方案2】:

只需使用“wrap_content”创建父布局和所需大小的子布局:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:layout_
              android:layout_
              android:orientation="vertical">

    <!-- This layout defines the desired size -->
    <LinearLayout
        android:layout_
        android:layout_
        android:orientation="vertical">

        <!-- Your views here -->

    </LinearLayout>

</LinearLayout>

【讨论】:

【参考方案3】:

如果您想显示具有固定大小(高度或宽度)的自定义窗口,则必须将其设为可绘制并将其设置为 XML 的背景。

我已经用解决方案发布了我的答案HERE。您可以参考它的工作原理。

【讨论】:

其实真正的解决方案是使用drawable作为背景。指定 layout_width 本身并没有帮助! 这不可能,这似乎太违反直觉了。如果它是正确的,那么这是一个疯狂的实现。 Grrrr,我认为 Google 聘请了最好的程序员,可惜他们不像我们其他人那样“思考”。【参考方案4】:
@Override
public View getInfoWindow(Marker arg0) 
   return null;


@Override
public View getInfoContents(Marker arg0) 
   View v = getActivity().getLayoutInflater().inflate(R.layout.custom_info_window, null);
   v.setLayoutParams(new LinearLayout.LayoutParams(500,300));

【讨论】:

如果我只想设置宽度静态而不是高度怎么办?高度应该是包装内容。我怎样才能做到这一点?有什么想法吗?【参考方案5】:

在我的例子中,我设置了布局的其中一个子元素的固定宽度并且它起作用了。不知道为什么在设置根视图的宽度时它不起作用。这是一个例子:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_
    android:layout_
    android:background="@color/white"
    android:orientation="vertical"
    android:padding="3dp">

    <ImageView
        android:id="@+id/info_window_image"
        android:layout_
        android:layout_
        android:scaleType="fitXY"
        android:src="@drawable/facebook"/>

    <TextView
        android:layout_
        android:layout_
        android:lines="1"
        android:maxLines="1"
        android:singleLine="true"
        android:text="Free for All Soccer Game"
        android:textColor="@color/blue"
        android:textSize="@dimen/medium" />
</LinearLayout

>

【讨论】:

以上是关于在 Google Maps api v2 中设置自定义 InfoWindow 的宽度的主要内容,如果未能解决你的问题,请参考以下文章