给地图标记添加圆角边缘

6

我正在使用InfoWindowAdapter为我的标记添加自定义信息窗口。除了一件事,我找不到将信息窗口设置为圆角的方法。

我正在使用谷歌地图V2。任何与此相关的代码片段都将非常有帮助。


您想显示带圆角的信息窗口还是想要在标记点处显示圆形? - TheFlash
@Pratik 我想展示带有圆角的信息窗口。 - Abx
等一下,我马上会发布答案。 - TheFlash
1
试试我的代码...如果有帮助,请告诉我...有人给了你-1..所以尝试将您的问题发布为“如何显示带圆角的信息窗口?”在删除问题之前请通知我。 - TheFlash
3个回答

13
 gMap.setInfoWindowAdapter(new InfoWindowAdapter() {

        @Override
        public View getInfoWindow(Marker arg0) 
        {
            View vMapInfo = ((Activity) context).getLayoutInflater().inflate(R.layout.map_info_layout, null);
            return vMapInfo;
        }

        @Override
        public View getInfoContents(Marker arg0) 
        {
            //View v = getLayoutInflater().inflate(R.layout.map_info_layout, null);
            return null;            

        }
    });

map_info_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:background="@drawable/bg_map_info"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:gravity="center_horizontal"
 >

<RelativeLayout
    android:layout_width="250dp"
    android:layout_height="45dp"
    android:background="@drawable/bg_map_info"
    android:paddingTop="0dp"
    android:paddingBottom="0dp"
    android:paddingLeft="5dp"
    android:paddingRight="5dp"
     >

    <TextView
        android:id="@+id/tvMapStoreName"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="3dp"
        android:layout_marginTop="2dp"
        android:layout_marginLeft="5dp"
        android:layout_marginRight="5dp"
        android:layout_toLeftOf="@+id/ivMapGo"
        android:ellipsize="end"
        android:lines="1"
        android:text="test text test text test text test text "
        android:textColor="#ffffff"
        android:textSize="@dimen/txt_size_common"
        android:textStyle="bold" />

    <ImageView
        android:id="@+id/ivMapGo"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_centerVertical="true"
        android:src="@drawable/ic_launcher" />

    <TextView
        android:id="@+id/tvMapStoreDistance"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/tvMapStoreName"
        android:layout_below="@+id/tvMapStoreName"
        android:lines="1"
        android:text="100 Miles"
        android:textColor="#FFFFFF"
        android:textSize="12dp"
        android:textStyle="normal" />

</RelativeLayout>



</LinearLayout>

以下是信息窗口背景的XML代码:

bg_map_info.xml

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item> 
    <shape android:shape="rectangle">
        <gradient android:angle="-90" android:startColor="#a0333333"
            android:endColor="#a0000000" />
        <corners 
            android:bottomLeftRadius="5dp"
            android:bottomRightRadius="5dp"
            android:topLeftRadius="5dp" 
            android:topRightRadius="5dp" />         

    </shape>
</item>
<item android:top="20dp"> 
    <shape android:shape="rectangle">
        <gradient android:angle="-90" android:startColor="#70000000"
            android:endColor="#70000000" />
        <corners 
            android:bottomLeftRadius="5dp"
            android:bottomRightRadius="5dp"
            android:topLeftRadius="5dp" 
            android:topRightRadius="5dp" />     </shape>
</item>
</layer-list>

1
我尝试了你的代码,但它所做的是显示一个有方边的视图,并在该视图内提供一个圆角的视图。 - Abx
1
@TharakaNirmana,如果您的布局与我的答案不同,能否分享一下? - TheFlash
1
@TharakaNirmana 你好,我刚刚检查了一下,它可以工作。请将宽度和高度设置为wrap_content,并给根布局添加一些填充。 - TheFlash
@Flash 我尝试了,仍然不行。这是我的代码: googleMap.setInfoWindowAdapter(new InfoWindowAdapter() { public View getInfoWindow(Marker marker) { View v = getLayoutInflater().inflate(R.layout.customized_info_window, null); return v; } public View getInfoContents(Marker marker) {info_window_layout View v = getLayoutInflater().inflate(R.layout.customized_info_window, null); title TextView heading = (TextView) v.findViewById(R.id.heading); heading.setText("示例"); return v; } }); - TharakaNirmana
1
@TharakaNirmana 请将您的代码放在getInfoContents方法中并尝试。 - TheFlash
显示剩余3条评论

0

CustomInfoWindowLayout:

  <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@drawable/btn_bg_white"
    android:orientation="vertical"
    android:padding="@dimen/_10dp_">

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

        <TextView
            android:id="@+id/info_window_address"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:fontFamily="@font/yaldevi_medium"
            android:textColor="#454f63"
            android:textSize="@dimen/_12sp_" />

        <TextView
            android:id="@+id/info_window_lat_lng"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:layout_marginTop="@dimen/_6dp_"
            android:fontFamily="@font/yaldevi_semi_bold"
            android:textColor="#454f63"
            android:textSize="@dimen/_12sp_" />

       </LinearLayout>

    </LinearLayout>

btn_bg_white.xml

<?xml version="1.0" encoding="utf-8"?>
<ripple xmlns:android="http://schemas.android.com/apk/res/android"
android:color="#DAC9C9"
android:exitFadeDuration="@android:integer/config_shortAnimTime">

<item>
    <shape android:shape="rectangle">
        <solid android:color="@color/white" />
        <corners android:radius="8dp" />
    </shape>
</item>
<item android:id="@android:id/mask">
    <shape android:shape="rectangle">
        <solid android:color="@color/white" />
        <corners android:radius="@dimen/_8dp_" />
    </shape>
</item>
</ripple>

MapActivity.java:-

 mMap.setInfoWindowAdapter(new GoogleMap.InfoWindowAdapter() {
    @Nullable
    Override
    public View getInfoWindow(@NonNull Marker marker) {
    View myContentView = getLayoutInflater().inflate(
    R.layout.custom_info_window, null);
    TextView tvAddress = ((TextView) myContentView
    findViewById(R.id.info_window_address));
                
    tvAddress.setText(HelperMethods.getCompleteAddressString(MapsActivity.this, 
    marker.getPosition().latitude, marker.getPosition().longitude));
    TextView tvLatLng = ((TextView) myContentView
    findViewById(R.id.info_window_lat_lng));
                
    tvLatLng.setText(HelperMethods.convertDegMinsSecs(marker.getPosition().latitude, 
    marker.getPosition().longitude));
    return myContentView;
    }
l
            @Nullable
            @Override
            public View getInfoContents(@NonNull Marker marker) {

                return null;
            }
        });

0

如果您将您的 .xml 文件填充到 getInfoWindow() 方法中,它将正常工作。此方法允许您提供一个视图,该视图将用于整个信息窗口。


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