如何以完美的圆角显示 RelativeLayout (MapView)

How to display RelativeLayout (MapView) with perfectly rounded

我在主 Relative Layout

上使用 mapview

我想让mapView完全圆润。

我在互联网上搜索得到的答案是我应该制作新的可绘制对象 xml 并将其添加到布局中,我想让它四舍五入 setBackgorund(R.drawable.xxxx)

所以,我在 drawable 文件夹中创建了 layout_bg.xml

layout_bg.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item>
    <shape android:shape="rectangle">
        <!-- solid background -->
        <solid android:color="#ffffba54"/>
        <!-- border-->
        <stroke
            android:color="#ffff206a"
            android:width="2dp"
            />
        <corners android:radius="25dp"/>
        <!-- different corners with different radius-->
        <!--corners
            android:bottomLeftRadius="5dp"
            android:topLeftRadius="10dp"
            android:topRightRadius="15dp"
            android:bottomRightRadius="20dp"
            /-->
    </shape>
</item>

但它根本不起作用。 所以,我尝试以编程方式进行。

MainActivity.java

 setContentView(R.layout.activity_main);
 mapView = new MapView(this);
    ViewGroup mapViewContainer = (ViewGroup)  
    findViewById(R.id.map_view);
    mapViewContainer.setBackgroundDrawable(R.drawable.layout_bg); 
                           <--- Here I get error. because R.drawable.layout_bg is int

    Log.e("mp","mapView2");
    mapViewContainer.addView(mapView);

activity_main.xml

<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="11dp"
        android:layout_marginStart="11dp"
        android:layout_marginTop="85dp"
        android:text="aaaaaaaa" />

    <RelativeLayout
        android:id="@+id/map_view"
        android:layout_width="200dp"
        android:layout_height="200dp"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        />



</RelativeLayout>

使用(MainActivity.this).getResources().getDrawable(R.drawable.layout_bg)那么你就不会得到错误..!!