自定义控件(三)自定义圆角Button

自定义圆角button

自定义圆角Button,需两步:

1.在drawable文件夹下新建 button_shape.xml文件
内容示例如下:

<?xml version="1.0" encoding="UTF-8"?> 
<shape 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:shape="rectangle"> 
    <!-- 填充的颜色 --> 
    <solid android:color="#FFFFFF" /> 
    <!-- 设置按钮的四个角为弧形 --> 
    <!-- android:radius 弧形的半径 --> 
    <corners android:radius="5dip" /> 

<!-- padding:Button里面的文字与Button边界的间隔 --> 
    <padding 
       android:left="10dp" 
       android:top="10dp" 
       android:right="10dp" 
       android:bottom="10dp" 
     /> 
</shape> 

2.在Activity的xml文件中将Button背景设置成刚刚创建的文件的背景。
示例:

<!--main_activity.xml-->

<Button   
    android:id="@+id/roundButton" 
    android:text=" 圆角按钮 " 
    android:layout_width="wrap_content"   
    android:layout_height="wrap_content"   
    android:background="@drawable/button_shape" 
    /> 

自定义圆形button

示例:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" 
    android:shape="oval">
    <solid android:color="#B40404"/>
    <corners android:radius="360dip"/>
</shape>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在 Winform 中,你可以通过继承现有控件的方式,来自定义一个控件。以下是一个简单的示例,演示如何自定义一个带有圆角按钮控件: ```csharp using System.Drawing; using System.Drawing.Drawing2D; using System.Windows.Forms; class RoundedButton : Button { public int CornerRadius { get; set; } = 16; protected override void OnPaint(PaintEventArgs pevent) { Graphics graphics = pevent.Graphics; graphics.SmoothingMode = SmoothingMode.AntiAlias; using (GraphicsPath path = new GraphicsPath()) { int x = CornerRadius; int y = CornerRadius; int width = Width - 1 - CornerRadius; int height = Height - 1 - CornerRadius; path.AddArc(x, y, CornerRadius, CornerRadius, 180, 90); path.AddArc(width, y, CornerRadius, CornerRadius, 270, 90); path.AddArc(width, height, CornerRadius, CornerRadius, 0, 90); path.AddArc(x, height, CornerRadius, CornerRadius, 90, 90); path.CloseAllFigures(); graphics.FillPath(new SolidBrush(BackColor), path); graphics.DrawPath(new Pen(ForeColor), path); } StringFormat stringFormat = new StringFormat(); stringFormat.LineAlignment = StringAlignment.Center; stringFormat.Alignment = StringAlignment.Center; graphics.DrawString(Text, Font, new SolidBrush(ForeColor), ClientRectangle, stringFormat); } } ``` 在上面的代码中,我们自定义了一个 `RoundedButton` 类,继承自 `Button` 控件,并重写了 `OnPaint` 方法以实现圆角效果。`CornerRadius` 属性用于设置圆角的半径大小。我们使用了 `GraphicsPath` 类来绘制圆角矩形,并使用 `SmoothingMode.AntiAlias` 属性来实现抗锯齿效果。 使用自定义控件时,你可以像普通控件一样将其添加到窗体中,并设置它的属性。例如: ```csharp RoundedButton button1 = new RoundedButton(); button1.Location = new Point(100, 100); button1.Size = new Size(150, 50); button1.Text = "My Button"; button1.CornerRadius = 24; this.Controls.Add(button1); ``` 在上面的代码中,我们创建了一个 `RoundedButton` 类型的控件,并将其添加到窗体中。我们设置了它的位置、大小、文本和圆角半径属性。当用户单击该按钮时,相应的代码会被执行。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值