Android 带透明内框的圆形内角
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11224464/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Rounded Inner corners with transparent inside frame
提问by user606669
I am trying to make a frame from code so that I can apply it to make rounded inner corners with a solid fill outside and transparent inside. Just like a solid rectangle with transparent oval inside.picture attached. I have tried few shape combinations all that available online displays the corners outside.
我正在尝试用代码制作一个框架,以便我可以应用它来制作圆形的内角,外面是实心填充,里面是透明的。就像一个实心矩形,里面有透明的椭圆形。附上图片。我尝试了几种形状组合,所有可用的在线显示在外面的角落。
The inside should be transparent not white. The image is taken from this postbut the solution presented here is not what I am looking for I dont want to use a 9 patch drawable but would like to be created in code.
里面应该是透明的而不是白色的。该图像取自这篇文章,但此处提供的解决方案不是我正在寻找的解决方案我不想使用 9 patch drawable 但希望在代码中创建。
Please valid answers only.
请仅提供有效答案。
回答by Nima K
create the following rounded_corner.xml:
创建以下 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>
add this below your imageView, which you want to apply the frame on it:
将此添加到您要在其上应用框架的 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" />
回答by Android Stack
First of all, create 3 xml
layout
in drawable folder:
首先,xml
layout
在drawable文件夹中创建3个:
- First: frame.xml
- Second: frame_build.xml
- Third: red.xml
- 第一个: frame.xml
- 第二个:frame_build.xml
- 第三: red.xml
(You can change this name as you wish),
(您可以根据需要更改此名称),
frame.xml:
框架.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>
frame_build.xml :
frame_build.xml :
<?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>
red.xml
红色文件
<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
<stroke android:width="40dp" android:height="40dp" android:color="#B22222" />
<padding android:left="8dp" android:top="-1dp" android:right="8dp" android:bottom="9dp" />
<corners android:radius="-10dp" />
</shape>
Finally refer your view or layout to Frame XML as follow :
最后将您的视图或布局引用到 Frame XML,如下所示:
android:background="@drawable/frame"
This tested and output as below image:
这经过测试并输出如下图:
Hope this help .
希望这有帮助。
回答by Zain
tweaking @Nima K solution, to avoid using an extra View
调整@Nima K 解决方案,以避免使用额外的视图
create frame.xml @ drawable
创建 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>
Then use it with 'android:background' attribute of your view
然后将它与视图的 'android:background' 属性一起使用
<TextView
android:id="@+id/text"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/frame" />