Android - 仅在顶部可绘制圆角
我有这个可绘制的圆角矩形作为背景:
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="@color/white" />
<stroke android:width="1dp" android:color="@color/light_gray" />
<padding android:left="10dp" android:top="10dp" android:right="10dp" android:bottom="10dp" />
<corners android:radius="6dp" />
</shape>
这工作正常,正如预期的那样。
现在,我想将其更改为仅将顶角变圆,因此我将其更改为:
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="@color/white" />
<stroke android:width="1dp" android:color="@color/light_gray" />
<padding android:left="10dp" android:top="10dp" android:right="10dp" android:bottom="10dp" />
<corners android:topLeftRadius="6dp" android:topRightRadius="6dp"
android:bottomLeftRadius="0dp" android:bottomRightRadius="0dp"/>
</shape>
但现在没有一个角是圆角的,并且我得到了一个普通的矩形。我在这里缺少什么?
I had this drawable to have a rounded rectangle as a background:
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="@color/white" />
<stroke android:width="1dp" android:color="@color/light_gray" />
<padding android:left="10dp" android:top="10dp" android:right="10dp" android:bottom="10dp" />
<corners android:radius="6dp" />
</shape>
This is working fine, as expected.
Now, I want to change this to only round the top corners, so I change it to this:
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="@color/white" />
<stroke android:width="1dp" android:color="@color/light_gray" />
<padding android:left="10dp" android:top="10dp" android:right="10dp" android:bottom="10dp" />
<corners android:topLeftRadius="6dp" android:topRightRadius="6dp"
android:bottomLeftRadius="0dp" android:bottomRightRadius="0dp"/>
</shape>
But now none of the corners are rounded and I get a plain rectangle. What am I missing here?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(10)
尝试给出这些值:
请注意,我已将
0dp
更改为0.1dp
。编辑:请参阅下面的 Aleks G 评论以获得更清晰的版本
Try giving these values:
Note that I have changed
0dp
to0.1dp
.EDIT: See Aleks G comment below for a cleaner version
就我而言,下面的代码
In my case below code
尝试做这样的事情:
似乎不适合设置不同的矩形角半径。所以你可以使用这个技巧。
Try to do something like this:
It seems does not suitable to set different corner radius of rectangle. So you can use this hack.
添加到您的布局中:
add in your layout:
尝试使用
MaterialShapeDrawable
并在 kotlin/java 代码中配置它。PS:
除了
xml
drawables 提供的功能(fillColor、描边)。 ..),MaterialShapeDrawable
支持:cornerFamily
分为两个类别:rounded
和cut
edgeTreatment
> 与TriangleEdgeTreatment
、OffsetEdgeTreatment
、...]
try to use
MaterialShapeDrawable
and configure it in kotlin/java code.P.S:
In addition to abilities that
xml
drawables provides (fillColor, stroke...),MaterialShapeDrawable
supports:cornerFamily
in two category:rounded
andcut
edgeTreatment
withTriangleEdgeTreatment
,OffsetEdgeTreatment
, ...]
基于 busylee 的回答,您可以通过以下方式制作一个只有一个 drawable 的方法: >un圆角(在本例中为左上角):
请注意,上面的
drawable
在Android Studio预览中未正确显示(2.0.0p7)。要预览它,请创建另一个视图并将其用作android:background="@drawable/..."
。Building upon busylee's answer, this is how you can make a
drawable
that only has one unrounded corner (top-left, in this example):Please note that the above
drawable
is not shown correctly in the Android Studio preview (2.0.0p7). To preview it anyway, create another view and use this asandroid:background="@drawable/..."
.您可能需要阅读此https://developer.android.com/ guide/topics/resources/drawable-resource.html#Shape
下面有一个注释。
注意 每个角(最初)都必须提供大于 1 的角半径,否则不会有任何角是圆角的。如果您不希望特定的角为圆角,解决方法是使用 android:radius 设置大于 1 的默认角半径,然后用您真正想要的值覆盖每个角,提供零(“0dp”) )您不需要圆角的地方。
You may need read this https://developer.android.com/guide/topics/resources/drawable-resource.html#Shape
and below there is a Note.
Note Every corner must (initially) be provided a corner radius greater than 1, or else no corners are rounded. If you want specific corners to not be rounded, a work-around is to use android:radius to set a default corner radius greater than 1, but then override each and every corner with the values you really want, providing zero ("0dp") where you don't want rounded corners.
在可绘制对象上创建 roung_top_corners.xml 并复制以下代码
Create roung_top_corners.xml on drawable and copy the below code
我尝试了你的代码并得到了一个顶部圆角按钮。我将颜色指定为
@ffffff
,并将描边指定为#C0C0C0
。尝试
这是我的输出。
I tried your code and got a top rounded corner button. I gave the colors as
@ffffff
and stroke I gave#C0C0C0
.try
this is my output.
尝试完全删除这些属性。
Try removing these attributes altogether.