控件的外边框怎么做,不是内边框


转载请注明出处,谢谢:http://blog.csdn.net/harryweasley/article/details/46906681


提前声明:本篇博客是基于电视机顶盒的,全部操作是用遥控器。




先看下效果图,选中后,edittext有边框。







如果所示,当ui想让我们做出这样的效果,大家肯定都很容易的做的出来,直接改变背景,就可以了,如下所示:

layout中,editText加入background就可以了。

<EditText
            android:id="@+id/edittext_search"
            android:layout_width="@dimen/edit_text_width"
            android:layout_height="46dp"
            android:layout_marginLeft="@dimen/search_grid_left"
            android:layout_marginRight="@dimen/search_grid_left"
            android:layout_marginTop="@dimen/search_edittext_top"
            android:background="@drawable/background_edittext_selector"
            android:paddingLeft="10dp"
            android:textColor="@color/white"
            android:textSize="@dimen/text_detail_app_name" />

在res/drawable下,建立background_edittext_selector.xml文件,如下所示:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
    <item android:state_focused="true"  android:drawable="@drawable/edittext_background"></item>

</selector>

在res/drawable下,edittext_background.xml文件如下所示:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >

    <!-- 内部颜色 -->
    <solid android:color="@color/edittext_bg" />
    <!-- 边缘线条颜色 -->
    <stroke android:width="3dp"
        android:color="@color/selected_bg" />
    <!-- 圆角的幅度 -->
    <corners android:radius="0dp" />

</shape>

这样就做出来效果了。


但是,但是,ui说,你这样做出来的是内边框,我想要的是外边框啊。


那么这应该怎么做呢,还是以上的思路。当editText没有焦点的时候,我们给他设置边缘线条宽度6dp,颜色不设置,当editText获取到焦点是,我们给他边缘线宽度设置为3dp,并且设置颜色。哈哈,这样就解决了。

 <EditText
            android:id="@+id/edittext_search"
            android:layout_width="@dimen/edit_text_width"
            android:layout_height="46dp"
            android:layout_marginLeft="@dimen/search_grid_left"
            android:layout_marginRight="@dimen/search_grid_left"
            android:layout_marginTop="@dimen/search_edittext_top"
            android:background="@drawable/background_edittext_selector"
            android:paddingLeft="10dp"
            android:textColor="@color/white"
            android:textSize="@dimen/text_detail_app_name" />

这里设置两个drawable,宽度和颜色的不同。

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
    <item android:state_focused="true"  android:drawable="@drawable/selected_edittext_background"></item>
    <item   android:drawable="@drawable/edittext_background"></item>

</selector>

edittext_background没有获取焦点的时候,边缘线条为6dp,并且不设置颜色。

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >

    <!-- 内部颜色 -->
    <solid android:color="@color/edittext_bg" />
    <!-- 边缘线条颜色 -->
    <stroke android:width="6dp" />
    <!-- 圆角的幅度 -->
    <corners android:radius="0dp" />

</shape>

selected_edittext_background获取焦点后,如下所示:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
    <!-- 内部颜色 -->  
    <solid android:color="@color/edittext_bg" />  
    <!-- 边缘线条颜色 -->  
    <stroke  
        android:width="3dp"  
        android:color="@color/selected_bg" />  
    <!-- 圆角的幅度 -->  
    <corners  
        android:radius="0dp" />  

</shape>





  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
要在WinForm中实现Win XP风格的窗口,你需要使用自定义绘制来绘制窗口的边框和标题栏。下面是一个简单的步骤: 1. 创建一个继承自 System.Windows.Forms.Form 的自定义窗体。 2. 设置窗体的 ControlBox 属性为 false,这将隐藏窗体的标准控件(最小化、最大化、关闭按钮)。 3. 重写 OnPaint 方法并绘制窗体的边框和标题栏。你可以使用 System.Windows.Forms.ControlPaint 类的 DrawBorder3D 和 DrawCaption 方法来帮助你绘制窗口的观。 4. 在 OnMouseDown、OnMouseMove 和 OnMouseUp 方法中处理用户的鼠标事件,以实现拖动窗口和调整窗口大小的功能。 5. 在 OnResize 方法中重新计算窗体的大小和位置,并调用 Invalidate 方法来触发重新绘制窗口。 下面是一些示例代码,可以帮助你入门: ```csharp public class CustomForm : System.Windows.Forms.Form { private const int WM_NCHITTEST = 0x84; private const int HTCLIENT = 0x1; private const int HTCAPTION = 0x2; private const int HTLEFT = 0x10; private const int HTRIGHT = 0x11; private const int HTTOP = 0x12; private const int HTTOPLEFT = 0x13; private const int HTTOPRIGHT = 0x14; private const int HTBOTTOM = 0x15; private const int HTBOTTOMLEFT = 0x16; private const int HTBOTTOMRIGHT = 0x17; private Rectangle _captionRect; private Rectangle _borderRect; public CustomForm() { this.ControlBox = false; this.DoubleBuffered = true; } protected override void OnPaint(PaintEventArgs e) { base.OnPaint(e); // 绘制窗口边框和标题栏 _captionRect = new Rectangle(0, 0, this.Width, 30); _borderRect = new Rectangle(0, 0, this.Width - 1, this.Height - 1); ControlPaint.DrawBorder3D(e.Graphics, _borderRect, Border3DStyle.Sunken); ControlPaint.DrawCaption(e.Graphics, _captionRect, CaptionText, Font, SystemColors.ActiveCaptionText, SystemColors.ActiveCaption, TextFormatFlags.VerticalCenter | TextFormatFlags.HorizontalCenter); // 绘制客户区域 Rectangle clientRect = new Rectangle(1, _captionRect.Height + 1, this.Width - 2, this.Height - _captionRect.Height - 2); e.Graphics.FillRectangle(Brushes.White, clientRect); } protected override void OnMouseDown(MouseEventArgs e) { base.OnMouseDown(e); // 处理鼠标按下事件,实现拖动窗口和调整窗口大小的功能 if (e.Button == MouseButtons.Left && _captionRect.Contains(e.Location)) { ReleaseCapture(); SendMessage(Handle, WM_NCLBUTTONDOWN, HTCAPTION, 0); } else if (e.Button == MouseButtons.Left && _borderRect.Contains(e.Location)) { if (e.Location.X < 5) { ReleaseCapture(); SendMessage(Handle, WM_NCLBUTTONDOWN, HTLEFT, 0); } else if (e.Location.X > this.Width - 5) { ReleaseCapture(); SendMessage(Handle, WM_NCLBUTTONDOWN, HTRIGHT, 0); } else if (e.Location.Y < _captionRect.Height + 5) { ReleaseCapture(); SendMessage(Handle, WM_NCLBUTTONDOWN, HTTOP, 0); } else if (e.Location.Y > this.Height - 5) { ReleaseCapture(); SendMessage(Handle, WM_NCLBUTTONDOWN, HTBOTTOM, 0); } else if (e.Location.X < _captionRect.Height + 5 && e.Location.Y < _captionRect.Height + 5) { ReleaseCapture(); SendMessage(Handle, WM_NCLBUTTONDOWN, HTTOPLEFT, 0); } else if (e.Location.X > this.Width - 5 && e.Location.Y < _captionRect.Height + 5) { ReleaseCapture(); SendMessage(Handle, WM_NCLBUTTONDOWN, HTTOPRIGHT, 0); } else if (e.Location.X < _captionRect.Height + 5 && e.Location.Y > this.Height - 5) { ReleaseCapture(); SendMessage(Handle, WM_NCLBUTTONDOWN, HTBOTTOMLEFT, 0); } else if (e.Location.X > this.Width - 5 && e.Location.Y > this.Height - 5) { ReleaseCapture(); SendMessage(Handle, WM_NCLBUTTONDOWN, HTBOTTOMRIGHT, 0); } } } protected override void OnMouseMove(MouseEventArgs e) { base.OnMouseMove(e); // 更改鼠标指针的形状,以指示用户可以拖动窗口或调整窗口大小 if (_captionRect.Contains(e.Location)) { Cursor = Cursors.SizeAll; } else if (_borderRect.Contains(e.Location)) { if (e.Location.X < 5 || e.Location.X > this.Width - 5) { Cursor = Cursors.SizeWE; } else if (e.Location.Y < _captionRect.Height + 5 || e.Location.Y > this.Height - 5) { Cursor = Cursors.SizeNS; } else if (e.Location.X < _captionRect.Height + 5 && e.Location.Y < _captionRect.Height + 5 || e.Location.X > this.Width - 5 && e.Location.Y < _captionRect.Height + 5 || e.Location.X < _captionRect.Height + 5 && e.Location.Y > this.Height - 5 || e.Location.X > this.Width - 5 && e.Location.Y > this.Height - 5) { Cursor = Cursors.SizeNWSE; } else { Cursor = Cursors.Default; } } else { Cursor = Cursors.Default; } } protected override void OnMouseUp(MouseEventArgs e) { base.OnMouseUp(e); // 还原鼠标指针的形状 Cursor = Cursors.Default; } protected override void OnResize(EventArgs e) { base.OnResize(e); // 触发重新绘制窗口 Invalidate(); } protected override CreateParams CreateParams { get { CreateParams cp = base.CreateParams; cp.Style |= 0x20000; // WS_MINIMIZEBOX return cp; } } [DllImport("user32.dll")] private static extern bool ReleaseCapture(); [DllImport("user32.dll")] private static extern IntPtr SendMessage(IntPtr hWnd, int Msg, int wParam, int lParam); } ``` 这是一个简单的例子,你可以根据自己的需要进行修改和扩展。
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值