Adding rounded corners to a mapview

那年仲夏 提交于 2019-11-27 03:46:16

问题


I'm trying to code a simple MapActivity which uses the following layout (only contains the mapview for the moment, as I plan to add share the screen with a ListView in the future) :

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

    <com.google.android.maps.MapView
    android:id="@+id/mapview"
    android:layout_width="fill_parent"
    android:layout_height="200dip"
    android:enabled="true"
    android:clickable="true"
    android:apiKey="mykey"
    android:layout_alignParentBottom="true" />
</RelativeLayout>

I have applied a simple shape to the mapview in order to have round corners and a stroke :

<?xml version="1.0" encoding="UTF-8"?> 
<shape xmlns:android="http://schemas.android.com/apk/res/android" 
     android:shape="rectangle"> 
     <corners android:bottomRightRadius="10dp"
              android:bottomLeftRadius="10dp" 
              android:topLeftRadius="10dp"
              android:topRightRadius="10dp"/> 
     <stroke  android:width="3dp"
              android:color="@color/bordure_tables" />
</shape>

I use the following to apply the shape :

mapView.setBackgroundResource(R.layout.map);

Problem is that it does not have any effect and I can't see my rounded corners, nor the stroke.


回答1:


Just wrote a tutorial to have rounded corners on a MapView it's done programmatically so should scale well to all devices:

http://www.anddev.org/novice-tutorials-f8/rounded-corners-map-view-t56908.html

Once you have added the custom view class you can just instantiate a rounded mapview like so:

<com.blundell.tut.ui.widget.RoundedMapView
   android:layout_width="fill_parent"
   android:layout_height="fill_parent"/>



回答2:


Have you tried assigning it a padding?

<padding 
     android:left="2dp" 
     android:top="2dp" 
     android:right="2dp" 
     android:bottom="2dp" /> 

So your strokes and radius should be visible.




回答3:


So I've achieved this affect by doing following: I've created a 9-patch drawable called round_corners and placed it in my drawable folder, then I use following xml to plot the map:

    <FrameLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">/

        <com.google.android.maps.MapView
            android:id="@+id/center_map"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:clickable="true"
            android:apiKey="your_key"
        />

        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:orientation="vertical"
            android:background="@drawable/round_corners"  
        ></LinearLayout>
    </FrameLayout>



回答4:


I was able to achieve this creating a rounded drawable like this:

<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
    <solid android:color="@android:color/transparent" />
    <stroke android:width="10dp" android:color="@color/color_gray5_eeeeee" />
    <corners android:radius="10px" />
</shape>

then using a linear layout and a margin of the same size of the drawable stroke width, like this:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/background_gray_rectangle_rounded_corners"
android:orientation="vertical">

<com.google.android.gms.maps.MapView
    android:id="@+id/map_details_map_view"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_marginTop="10dp"
    android:layout_marginBottom="10dp"
    android:clickable="true"
    android:enabled="true" />
</LinearLayout>



回答5:


Just put the Mapview into a CardView

 <android.support.v7.widget.CardView
        android:layout_width="match_parent"
        android:layout_height="100dp"
        android:elevation="0dp"
        app:cardCornerRadius="5dp">

     <com.google.android.gms.maps.MapView
            android:layout_width="match_parent"
            android:layout_height="match_parent"/>

 </android.support.v7.widget.CardView>


来源:https://stackoverflow.com/questions/5211731/adding-rounded-corners-to-a-mapview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!