Android 圆角内角,内部框架透明

Android 圆角内角,内部框架透明,android,Android,我试图从代码中创建一个框架,这样我就可以应用它来创建内部圆角,外部为实心填充,内部为透明填充就像一个实心矩形,内部有透明的椭圆形。图片已附。我尝试过几种形状组合,所有在线可用的形状组合都会显示外角 内部应该是透明的,而不是白色的。 这张图片是从这里拍摄的,但这里提供的解决方案不是我想要的,我不想使用9补丁,但希望在代码中创建 请仅填写有效答案。首先,在可绘图文件夹中创建3个xml布局: 第一个:frame.xml 第二:frame\u build.xml 第三:red.xml (您可以根据需要

我试图从代码中创建一个框架,这样我就可以应用它来创建内部圆角,外部为实心填充,内部为透明填充就像一个实心矩形,内部有透明的椭圆形。图片已附。我尝试过几种形状组合,所有在线可用的形状组合都会显示外角

内部应该是透明的,而不是白色的。 这张图片是从这里拍摄的,但这里提供的解决方案不是我想要的,我不想使用9补丁,但希望在代码中创建


请仅填写有效答案。

首先,在可绘图文件夹中创建3个
xml
布局:

  • 第一个:frame.xml
  • 第二:frame\u build.xml
  • 第三:red.xml
  • (您可以根据需要更改此名称)

    frame.xml:

    <?xml version="1.0" encoding="UTF-8"?>
    <layer-list xmlns:android="http://schemas.android.com/apk/res/android">
        <item android:bottom="20dp" android:drawable="@drawable/red" android:top="-25dp" />
        <item android:bottom="15dp" android:drawable="@drawable/frame_build" android:top="5dp" android:left="-5dp" android:right="-5dp" />
    </layer-list>
    
    <?xml version="1.0" encoding="UTF-8"?>
    <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
        <padding android:left="10dp" android:top="10dp" android:right="10dp" android:bottom="10dp" />
        <corners android:radius="40dp" />
    </shape>
    
    此测试和输出如下图所示:


    希望有此帮助。

    创建以下rounded_corner.xml:

    <?xml version="1.0" encoding="utf-8"?>
    <layer-list xmlns:android="http://schemas.android.com/apk/res/android">
        <item
            android:bottom="-10dp"
            android:left="-10dp"
            android:right="-10dp"
            android:top="-10dp">
            <shape android:shape="rectangle">
                <stroke
                    android:width="10dp"
                    android:color="#ffffff" />
                <corners android:radius="20dp" />
            </shape>
        </item>
    </layer-list>
    
    
    
    将其添加到要在其上应用框架的imageView下面:

    <View
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_alignBottom="@+id/my_image_view"
        android:layout_alignLeft="@id/my_image_view"
        android:layout_alignRight="@+id/my_image_view"
        android:layout_alignTop="@id/my_image_view"
        android:background="@drawable/rounded_corner" />
    

    调整@Nima K解决方案,以避免使用额外视图

    创建frame.xml@drawable

    <?xml version="1.0" encoding="utf-8"?>
    <layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    
        <item
            android:bottom="-10dp"
            android:left="-10dp"
            android:right="-10dp"
            android:top="-10dp">
            <shape android:shape="rectangle">
                <stroke
                    android:width="20dp"
                    android:color="@color/frame_color" />
                <corners android:radius="30dp" />
            </shape>
        </item>
    
        <item>
            <shape android:shape="rectangle">
                <solid android:color="@android:color/transparent" />
    
                <stroke
                    android:width="20dp"
                    android:color="@color/frame_color" />
    
                <corners android:radius="40dp" />
            </shape>
    
        </item>
    
    </layer-list>
    
    
    
    然后将其与视图的“android:background”属性一起使用

    <TextView
            android:id="@+id/text"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@drawable/frame" />
    
    
    


    也许更简单

    <inset xmlns:android="http://schemas.android.com/apk/res/android"
        android:inset="-6dp">
    
        <shape android:shape="rectangle">
            <solid android:color="#FFFFFF" />
    
            <stroke android:width="12dp"
                android:color="#000000" />
        
            <corners android:radius="16dp" />
        </shape>
    </inset>
    
    
    
    似乎正是我所需要的,让我试试,然后我会将其标记为answer@user606669很高兴帮助你我试过了,但背景是黑色的,不是透明的。我做错了什么。我正在framelayout中使用它,它应该显示它下面的框架。@user606669为framelayout和frame_build.XML添加相同的颜色,这将为您提供transparancy@AndroidStack你能解释一下数字之间的关系吗,还是用常数来代替?这是最好的答案。谢谢它工作得很好,但我不明白为什么,你能解释一下吗?它在覆盖矩形角落的矩形上画了一个更大的“椭圆形笔划”。这正是我一直在寻找的!
    <TextView
            android:id="@+id/text"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@drawable/frame" />
    
    <inset xmlns:android="http://schemas.android.com/apk/res/android"
        android:inset="-6dp">
    
        <shape android:shape="rectangle">
            <solid android:color="#FFFFFF" />
    
            <stroke android:width="12dp"
                android:color="#000000" />
        
            <corners android:radius="16dp" />
        </shape>
    </inset>