【问题标题】:Adding rounded corners to a mapview向地图视图添加圆角
【发布时间】:2019-02-13 18:52:34
【问题描述】:

我正在尝试编写一个使用以下布局的简单 MapActivity(目前仅包含地图视图,因为我计划将来添加与 ListView 共享屏幕):

<?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>

我为地图视图应用了一个简单的形状,以便有圆角和笔触:

<?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>

我使用以下方法来应用形状:

mapView.setBackgroundResource(R.layout.map);

问题是它没有任何效果,我看不到我的圆角,也看不到笔划。

【问题讨论】:

  • 同样的问题!请帮助我们。

标签: android android-mapview rounded-corners


【解决方案1】:

刚刚写了一个教程,在 MapView 上设置圆角,它是通过编程完成的,所以应该可以很好地扩展到所有设备:

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

添加自定义视图类后,您可以像这样实例化圆形地图视图:

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

【讨论】:

  • 完成组件后,这将是一个很好的解决方案:)
  • 链接正在链接到其他东西:(
  • 是的,摆脱了那个域,一些恶意软件偷窥了它。我的意思是这是从 2011 年开始的,但它仍然在这里 :) blog.blundellapps.co.uk/rounded-corners-mapview
【解决方案2】:

您是否尝试过为其分配填充?

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

所以你的笔画和半径应该是可见的。

【讨论】:

  • 这对mapview不起作用,它确实是一个无能的组件:)
【解决方案3】:

所以我通过执行以下操作实现了这种效果:我创建了一个名为 round_corners 的 9-patch 可绘制对象并将其放置在我的可绘制文件夹中,然后我使用以下 xml 来绘制地图:

    <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】:

    我能够像这样创建一个圆形可绘制对象来实现这一点:

    <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>
    

    然后使用与可绘制笔画宽度相同大小的线性布局和边距,像这样:

    <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】:

      只需将 Mapview 放入 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>
      

      【讨论】:

      • 欢迎来到 Stack Overflow。你能解释一下你的解决方案吗?源代码当然很好,但添加解释会大大提高对社区的长期价值。 Tips for Answering。谢谢。
      猜你喜欢
      • 1970-01-01
      • 2012-02-21
      • 1970-01-01
      • 2011-04-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-11-02
      • 2013-04-10
      相关资源
      最近更新 更多