如何在Android UI中绘制圆角矩形?
我需要在 Android UI 中绘制一个圆角矩形。对于 TextView
和 EditText
使用相同的圆角矩形也会很有帮助。
I need to draw a rounded rectangle in the Android UI. Having the same rounded rectangle for TextView
and EditText
would also be helpful.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(10)
在您的布局 xml 中执行以下操作:
通过更改
android:radius
您可以更改角的“半径”量。
用于定义可绘制对象的颜色。您可以将
android:radius
替换为android:bottomLeftRadius
、android:bottomRightRadius
、android:topLeftRadius
和>android:topRightRadius
定义每个角的半径。In your layout xml do the following:
By changing the
android:radius
you can change the amount of "radius" of the corners.<solid>
is used to define the color of the drawable.You can use replace
android:radius
withandroid:bottomLeftRadius
,android:bottomRightRadius
,android:topLeftRadius
andandroid:topRightRadius
to define radius for each corner.我想,这正是你所需要的。
这里的drawable(xml)文件创建圆角矩形。
round_rect_shape.xml
此处布局文件:my_layout.xml
->在上面的代码中,LinearLayout具有背景(这是创建圆角矩形的关键作用)。
因此,您可以在 LinearLayout 中放置任何视图,如 TextView、EditText...,以将背景视为所有圆角矩形。
I think, this is you exactly needed.
Here drawable(xml) file that creates rounded rectangle.
round_rect_shape.xml
Here layout file: my_layout.xml
-> In the above code, LinearLayout having the background(That is the key role to place to create rounded rectangle).
So you can place any view like TextView, EditText... in that LinearLayout to see background as round rectangle for all.
在
monodroid
中,你可以对圆角矩形进行这样的操作,然后将其作为父类,可以添加editbox
和其他布局功能。In
monodroid
, you can do like this for rounded rectangle, and then keeping this as a parent class,editbox
and other layout features can be added.现在,您想在哪个元素中使用此形状,只需添加:
android:background="@drawable/custom_round_ui_shape"
在名为“custom_round_ui_shape”的可绘制对象中创建一个新的 XML
Now, in which element you want to use this shape just add:
android:background="@drawable/custom_round_ui_shape"
Create a new XML in drawable named "custom_round_ui_shape"
右键单击可绘制对象并创建新的布局 xml 文件,其名称例如为“button_background.xml”。
然后复制并粘贴以下代码。
您可以根据需要更改它。
现在您可以使用它了。
Right_click on the drawable and create new layout xml file in the name of for example button_background.xml.
then copy and paste the following code.
You can change it according your need.
Now you can use it.
您可以在drawables文件夹中定义一个新的xml背景,
然后将其包含在您的TextView或EditText中
通过在后台定义它。
You could just define a new xml background in the drawables folder
After this just include it in your TextView or EditText
by defining it in the background.
使用 CardView 作为圆角矩形。 CardView 提供了更多功能,如 cardCornerRadius、cardBackgroundColor、cardElevation 和 cardCornerRadius。还有更多。 CardView 使 UI 比自定义圆矩形可绘制更合适。
Use CardView for Round Rectangle. CardView give more functionality like cardCornerRadius, cardBackgroundColor, cardElevation & many more. CardView make UI more suitable then Custom Round Rectangle drawable.
我尝试了几种方法,可以说您还应该添加 View.setClipToOutline (API >= 21):
在这种情况下您不会在
TextView
下面得到矩形选择(它发生在某些设备上),如下所示:有些人用
CardView
包裹一个矩形。它可以工作,但在某些旧设备上,如果半径足够大,它会显示错误的角:在
TextView
中,您还可以添加此属性:当单击浅灰色文本标签。
I tried several methods and can say that you should also add View.setClipToOutline (API >= 21):
In this case you won't get a rectangular selection below
TextView
(it occurs on some devices) like below:Some people wrap a rectangle with
CardView
. It works, but on some old devices it shows wrong corners, if radius is large enough:In
TextView
you can also add this attribute:It will show darker gray selection when click light gray text labels.