创建一个只有两个圆角边缘的矩形

发布于 2024-09-27 11:10:04 字数 138 浏览 4 评论 0原文

我可以创建一个所有边缘均为圆形的矩形形状。然而,我想要的是一个矩形,只有两个边缘是圆形的。这可能吗?

我本质上是在拼凑一个 ListView,它看起来像一个带有圆形边缘的气泡。我希望添加一个两个顶部边缘呈圆形的页眉和一个两个底部边缘呈圆形的页脚。

I can create a shape that is a rectangle with all edges rounded. However, what I'm wanting is a rectangle shape with only 2 of the edges rounded. Is this possible?

I'm essentially hacking together a ListView that looks like a bubble with rounded edges. I'm looking to add a header that has the two top edges rounded and a footer with the two bottom edges rounded.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

漫雪独思 2024-10-04 11:10:04
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" android:padding="10dp">
<solid android:color="#FFFFFF"/>
<corners 
android:bottomRightRadius="0dp"
android:bottomLeftRadius="0dp"
android:topLeftRadius="15dp"
android:topRightRadius="15dp"/>
</shape>

这段代码(从?)Android 版本 2.2 开始工作。参考文档,代码应该看起来像像下面这样:

<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" android:padding="10dp">
<solid android:color="#FFFFFF"/>
<corners 
android:radius="2dp"
android:bottomRightRadius="0dp"
android:bottomLeftRadius="0dp"
android:topLeftRadius="15dp"
android:topRightRadius="15dp"/>
</shape>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" android:padding="10dp">
<solid android:color="#FFFFFF"/>
<corners 
android:bottomRightRadius="0dp"
android:bottomLeftRadius="0dp"
android:topLeftRadius="15dp"
android:topRightRadius="15dp"/>
</shape>

This code is just working (since?) Android version 2.2. Referring to the documentation, the code should look like the following:

<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" android:padding="10dp">
<solid android:color="#FFFFFF"/>
<corners 
android:radius="2dp"
android:bottomRightRadius="0dp"
android:bottomLeftRadius="0dp"
android:topLeftRadius="15dp"
android:topRightRadius="15dp"/>
</shape>
孤独难免 2024-10-04 11:10:04

您可能会发现这很有帮助。

<?xml version="1.0" encoding="utf-8"?>
<!--  res/drawable/rounded_edittext.xml -->
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" android:padding="10dp">
 <solid android:color="#FFFFFF"/>
    <corners
     android:bottomRightRadius="0dp"
     android:bottomLeftRadius="0dp"
  android:topLeftRadius="15dp"
  android:topRightRadius="15dp"/>
</shape>

You might find this helpful.

<?xml version="1.0" encoding="utf-8"?>
<!--  res/drawable/rounded_edittext.xml -->
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" android:padding="10dp">
 <solid android:color="#FFFFFF"/>
    <corners
     android:bottomRightRadius="0dp"
     android:bottomLeftRadius="0dp"
  android:topLeftRadius="15dp"
  android:topRightRadius="15dp"/>
</shape>
歌枕肩 2024-10-04 11:10:04

我将 android:radius 属性更改为 android:topRightRadiusandroid:topLeftRadius

I changed the android:radius attribute to android:topRightRadius and android:topLeftRadius

花开半夏魅人心 2024-10-04 11:10:04

我认为最好的(根据我对你的问题的理解)是创建一个 9patch 图像并将其用作背景 - 可以定义为根据需要拉伸并在 XML 级别设置,从而节省一些代码工作

I think the best (to my understanding of your question) is to create a 9patch image and use it as background - can be defined to stretch as you need and set at the XML level saving some code work

久隐师 2024-10-04 11:10:04
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" android:padding="10dp">
<solid android:color="#FFFFFF"/>
<corners 
android:bottomRightRadius="0dp"
android:bottomLeftRadius="0dp"
android:topLeftRadius="15dp"
android:topRightRadius="15dp"/>
</shape>

使用上面的代码

<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" android:padding="10dp">
<solid android:color="#FFFFFF"/>
<corners 
android:bottomRightRadius="0dp"
android:bottomLeftRadius="0dp"
android:topLeftRadius="15dp"
android:topRightRadius="15dp"/>
</shape>

Use the above code

も让我眼熟你 2024-10-04 11:10:04

我会尝试绘制一个圆角矩形,然后绘制两个与圆角矩形的角半径相同大小的附加矩形,并将它们放置在您不想圆角的角上。

例如,一个半径为 15px 的圆角矩形,以及两个 15x15px 的常规矩形,放置在不应圆化的圆角矩形的角上。

编辑:重新阅读您的问题,您最好为列表视图的顶部和底部制作一个简单的 9 补丁图像。它们可以以这种方式拉伸到任何尺寸,而无需像素化。不过,这需要您事先创建图像,并且使用矩形形状进行操作会更容易在以后的代码中进行修改。然而,9 个补丁的方式就不那么麻烦了。

I would try drawing a rounded rectangle, and then two additional rectangles of the same size as the rounded rectangle's corner radius, and place them in the corners that you don't want to be rounded.

So for example, a rounded rectangle with a radius of 15px, and two regular rectangles 15x15px, placed into the corners of the rounded rectangle that should not be rounded.

EDIT: Re-reading your question, you might be better off making a simple 9-patch image for the top and bottom of your listview. They would stretch to any size that way without pixelization. That would require you to create the images beforehand, though, and doing things with the rectangle shapes would be easier to modify in code later. However, the 9-patch way would be less of a hack.

心是晴朗的。 2024-10-04 11:10:04

绘制一个圆角矩形,然后在圆角矩形上绘制一个具有相同颜色的普通矩形。

Draw a rounded rectangle and then draw a normal rectangle over the rounded one with the same color.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文