如何在Android UI中绘制圆角矩形?

发布于 2024-10-31 15:47:51 字数 98 浏览 1 评论 0原文

我需要在 Android UI 中绘制一个圆角矩形。对于 TextViewEditText 使用相同的圆角矩形也会很有帮助。

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 技术交流群。

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

发布评论

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

评论(10

娇纵 2024-11-07 15:47:51

在您的布局 xml 中执行以下操作:

<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">

    <solid android:color="@android:color/holo_red_dark" />

    <corners android:radius="32dp" />

</shape>

通过更改 android:radius 您可以更改角的“半径”量。

用于定义可绘制对象的颜色。

您可以将 android:radius 替换为 android:bottomLeftRadiusandroid:bottomRightRadiusandroid:topLeftRadius>android:topRightRadius 定义每个角的半径。

In your layout xml do the following:

<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">

    <solid android:color="@android:color/holo_red_dark" />

    <corners android:radius="32dp" />

</shape>

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 with android:bottomLeftRadius, android:bottomRightRadius, android:topLeftRadius and android:topRightRadius to define radius for each corner.

佞臣 2024-11-07 15:47:51

我想,这正是你所需要的。

这里的drawable(xml)文件创建圆角矩形。
round_rect_shape.xml

<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle" >

    <solid android:color="#ffffff" />

    <corners
        android:bottomLeftRadius="8dp"
        android:bottomRightRadius="8dp"
        android:topLeftRadius="8dp"
        android:topRightRadius="8dp" />

</shape>

此处布局文件:my_layout.xml

<LinearLayout
    android:id="@+id/linearLayout1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/round_rect_shape"
    android:orientation="vertical"
    android:padding="5dp" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Something text"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:textColor="#ff0000" />

    <EditText
        android:id="@+id/editText1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <requestFocus />
    </EditText>
</LinearLayout>

->在上面的代码中,LinearLayout具有背景(这是创建圆角矩形的关键作用)。
因此,您可以在 LinearLayout 中放置任何视图,如 TextView、EditText...,以将背景视为所有圆角矩形。

I think, this is you exactly needed.

Here drawable(xml) file that creates rounded rectangle.
round_rect_shape.xml

<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle" >

    <solid android:color="#ffffff" />

    <corners
        android:bottomLeftRadius="8dp"
        android:bottomRightRadius="8dp"
        android:topLeftRadius="8dp"
        android:topRightRadius="8dp" />

</shape>

Here layout file: my_layout.xml

<LinearLayout
    android:id="@+id/linearLayout1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/round_rect_shape"
    android:orientation="vertical"
    android:padding="5dp" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Something text"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:textColor="#ff0000" />

    <EditText
        android:id="@+id/editText1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <requestFocus />
    </EditText>
</LinearLayout>

-> 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.

素罗衫 2024-11-07 15:47:51

monodroid中,你可以对圆角矩形进行这样的操作,然后将其作为父类,可以添加editbox和其他布局功能。

 class CustomeView : TextView
    {
       public CustomeView (Context context, IAttributeSet ) : base (context, attrs)  
        {  
        }
       public CustomeView(Context context, IAttributeSet attrs, int defStyle) : base(context, attrs, defStyle)  
        {  
        }
       protected override void OnDraw(Android.Graphics.Canvas canvas)
       {
           base.OnDraw(canvas);
           Paint p = new Paint();
           p.Color = Color.White;
           canvas.DrawColor(Color.DarkOrange);

           Rect rect = new Rect(0,0,3,3);

           RectF rectF = new RectF(rect);


           canvas.DrawRoundRect( rectF, 1,1, p);



       }  
    }
}

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.

 class CustomeView : TextView
    {
       public CustomeView (Context context, IAttributeSet ) : base (context, attrs)  
        {  
        }
       public CustomeView(Context context, IAttributeSet attrs, int defStyle) : base(context, attrs, defStyle)  
        {  
        }
       protected override void OnDraw(Android.Graphics.Canvas canvas)
       {
           base.OnDraw(canvas);
           Paint p = new Paint();
           p.Color = Color.White;
           canvas.DrawColor(Color.DarkOrange);

           Rect rect = new Rect(0,0,3,3);

           RectF rectF = new RectF(rect);


           canvas.DrawRoundRect( rectF, 1,1, p);



       }  
    }
}
九局 2024-11-07 15:47:51
<shape xmlns:android="http://schemas.android.com/apk/res/android"
  android:padding="10dp"
  android:shape="rectangle">
    <solid android:color="@color/colorAccent" /> 
    <corners
      android:bottomLeftRadius="500dp"
      android:bottomRightRadius="500dp"
      android:topLeftRadius="500dp"
      android:topRightRadius="500dp" />
</shape>

现在,您想在哪个元素中使用此形状,只需添加:
android:background="@drawable/custom_round_ui_shape"

在名为“custom_round_ui_shape”的可绘制对象中创建一个新的 XML

<shape xmlns:android="http://schemas.android.com/apk/res/android"
  android:padding="10dp"
  android:shape="rectangle">
    <solid android:color="@color/colorAccent" /> 
    <corners
      android:bottomLeftRadius="500dp"
      android:bottomRightRadius="500dp"
      android:topLeftRadius="500dp"
      android:topRightRadius="500dp" />
</shape>

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"

坏尐絯 2024-11-07 15:47:51

右键单击可绘制对象并创建新的布局 xml 文件,其名称例如为“button_background.xml”。
然后复制并粘贴以下代码。
您可以根据需要更改它。

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<corners
android:radius="14dp" />
<solid android:color="@color/colorButton" />
<padding
android:bottom="0dp"
android:left="0dp"
android:right="0dp"
android:top="0dp" />
<size
android:width="120dp"
android:height="40dp" />
</shape>

现在您可以使用它了。

<Button
android:background="@drawable/button_background"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>

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.

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<corners
android:radius="14dp" />
<solid android:color="@color/colorButton" />
<padding
android:bottom="0dp"
android:left="0dp"
android:right="0dp"
android:top="0dp" />
<size
android:width="120dp"
android:height="40dp" />
</shape>

Now you can use it.

<Button
android:background="@drawable/button_background"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
⒈起吃苦の倖褔 2024-11-07 15:47:51

您可以在drawables文件夹中定义一个新的xml背景,

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="enter_your_desired_color_here" />
<corners android:radius="enter_your_desired_radius_the_corners" />
</shape>  

然后将其包含在您的TextView或EditText中
通过在后台定义它。

<TextView
 android:id="@+id/textView"
 android:layout_width="0dp"
 android:layout_height="80dp"
 android:background="YOUR_FILE_HERE"
 Android:layout_weight="1"
 android:gravity="center"
 android:text="TEXT_HERE"
 android:textSize="40sp" />

You could just define a new xml background in the drawables folder

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="enter_your_desired_color_here" />
<corners android:radius="enter_your_desired_radius_the_corners" />
</shape>  

After this just include it in your TextView or EditText
by defining it in the background.

<TextView
 android:id="@+id/textView"
 android:layout_width="0dp"
 android:layout_height="80dp"
 android:background="YOUR_FILE_HERE"
 Android:layout_weight="1"
 android:gravity="center"
 android:text="TEXT_HERE"
 android:textSize="40sp" />
倾城°AllureLove 2024-11-07 15:47:51

使用 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.

比忠 2024-11-07 15:47:51
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <solid android:color="@android:color/white" />
    <corners android:radius="4dp" />
</shape>
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <solid android:color="@android:color/white" />
    <corners android:radius="4dp" />
</shape>
埖埖迣鎅 2024-11-07 15:47:51

我尝试了几种方法,可以说您还应该添加 View.setClipToOutline (API >= 21):

textView.clipToOutline = true

在这种情况下您不会在 TextView 下面得到矩形选择(它发生在某些设备上),如下所示:

在此处输入图像描述

有些人CardView包裹一个矩形。它可以工作,但在某些旧设备上,如果半径足够大,它会显示错误的角:

在此处输入图像描述

TextView 中,您还可以添加此属性:

android:theme="@style/Theme.MaterialComponents.Light"

当单击浅灰色文本标签。

I tried several methods and can say that you should also add View.setClipToOutline (API >= 21):

textView.clipToOutline = true

In this case you won't get a rectangular selection below TextView (it occurs on some devices) like below:

enter image description here

Some people wrap a rectangle with CardView. It works, but on some old devices it shows wrong corners, if radius is large enough:

enter image description here

In TextView you can also add this attribute:

android:theme="@style/Theme.MaterialComponents.Light"

It will show darker gray selection when click light gray text labels.

左岸枫 2024-11-07 15:47:51
    paint.apply {
        strokeWidth = lineWidth.toFloat()
        style = Paint.Style.STROKE
        color = lineColor
        ***strokeCap = Paint.Cap.ROUND***
    }
    paint.apply {
        strokeWidth = lineWidth.toFloat()
        style = Paint.Style.STROKE
        color = lineColor
        ***strokeCap = Paint.Cap.ROUND***
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文