之前看到手机上的百度editText控件是圆角的就尝试做了一下,看了看相关的文章。

因为代码少,看看就知道了。所以下面我就直接贴上代码供大家参考,有其他的好方法记得分享哦~

整个代码不涉及JAVA代码,首先是需要被MainActivity加载的页面代码:

 <EditText
android:id="@+id/edt_operator_name" android:layout_width="250dip"
android:layout_height="wrap_content"
android:background="@layout/rounded_edittext"
android:gravity="center_vertical"
android:hint="百度"
android:paddingBottom="10dip"
android:paddingTop="10dip" />

然后就是具体的样式rounded_edittext.xml代码:

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

此时,运行即可。

EditText去边框:EditText的background属性设置为@null就搞定了:android:background="@null"。

其实以上内容的知识点就是shape的使用,下面简单的介绍一下:

shape用于设定形状,可以在selector,layout等里面使用,有6个子标签,各属性如下:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" > <!-- 圆角 -->
<corners
android:radius="9dp"
android:topLeftRadius="2dp"
android:topRightRadius="2dp"
android:bottomLeftRadius="2dp"
android:bottomRightRadius="2dp"/><!-- 设置圆角半径 --> <!-- 渐变 -->
<gradient
android:startColor="@android:color/white"
android:centerColor="@android:color/black"
android:endColor="@android:color/black"
android:useLevel="true"
android:angle="45"//android:angle 整型 渐变角度(PS:当angle=0时,渐变色是从左向右。 然后逆时针方向转,当angle=90时为从下往上。angle必须为45的整数倍)
android:type="radial"
android:centerX="0"
android:centerY="0"
android:gradientRadius="90"/> <!-- 间隔 -->
<padding
android:left="2dp"
android:top="2dp"
android:right="2dp"
android:bottom="2dp"/><!-- 各方向的间隔 --> <!-- 大小 -->
<size
android:width="50dp"
android:height="50dp"/><!-- 宽度和高度 --> <!-- 填充 -->
<solid
android:color="@android:color/white"/><!-- 填充的颜色 --> <!-- 描边 -->
<stroke
android:width="2dp"
android:color="@android:color/black"
android:dashWidth="1dp"
android:dashGap="2dp"/> </shape>

填充:设置填充的颜色

间隔:设置四个方向上的间隔

大小:设置大小

圆角:同时设置五个属性,则Radius属性无效

android:Radius="20dp"                           设置四个角的半径

android:topLeftRadius="20dp"              设置左上角的半径
android:topRightRadius="20dp"           设置右上角的半径
android:bottomLeftRadius="20dp"      设置右下角的半径
android:bottomRightRadius="20dp"    设置左下角的半径

描边:dashWidth和dashGap属性,只要其中一个设置为0dp,则边框为实现边框

android:width="20dp"                               设置边边的宽度
android:color="@android:color/black"  设置边边的颜色
android:dashWidth="2dp"                         设置虚线的宽度
android:dashGap="20dp"                          设置虚线的间隔宽度

渐变:当设置填充颜色后,无渐变效果。angle的值必须是45的倍数(包括0),仅在type="linear"有效,不然会报错。android:useLevel 这个属性不知道有什么用。

上面的圆角例子是layout作为背景,下面有一个selecter作为背景的例子(这个例子出自:http://www.oschina.net/question/166763_34833):

main.xml:

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TestShapeButton"
android:background="@drawable/button_selector"
/>

button_selector.xml:

<?xml version="1.0" encoding="utf-8"?>
<selector
xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" >
<shape>
<!-- 渐变 -->
<gradient
android:startColor="#ff8c00"
android:endColor="#FFFFFF"
android:type="radial"
android:gradientRadius="50" />
<!-- 描边 -->
<stroke
android:width="2dp"
android:color="#dcdcdc"
android:dashWidth="5dp"
android:dashGap="3dp" />
<!-- 圆角 -->
<corners
android:radius="2dp" />
<padding
android:left="10dp"
android:top="10dp"
android:right="10dp"
android:bottom="10dp" />
</shape>
</item> <item android:state_focused="true" >
<shape>
<gradient
android:startColor="#ffc2b7"
android:endColor="#ffc2b7"
android:angle="270" />
<stroke
android:width="2dp"
android:color="#dcdcdc" />
<corners
android:radius="2dp" />
<padding
android:left="10dp"
android:top="10dp"
android:right="10dp"
android:bottom="10dp" />
</shape>
</item> <item>
<shape>
<solid android:color="#ff9d77"/>
<stroke
android:width="2dp"
android:color="#fad3cf" />
<corners
android:topRightRadius="5dp"
android:bottomLeftRadius="5dp"
android:topLeftRadius="0dp"
android:bottomRightRadius="0dp"
/>
<padding
android:left="10dp"
android:top="10dp"
android:right="10dp"
android:bottom="10dp" />
</shape>
</item>
</selector>

此外,我还在网上找到了个渐变的实例,地址如下:

http://l62s.iteye.com/blog/1659433

Android中使用shape实现EditText圆角的更多相关文章

  1. Android中的shape

    在Android程序开发中,我们经常会去用到Shape这个东西去定义各种各样的形状,shape可以绘制矩形环形以及椭圆,所以只需要用椭圆即可,在使用的时候将控件比如imageview或textview ...

  2. Android中的Shape使用总结

    参考:http://www.cnblogs.com/gzggyy/archive/2013/05/17/3083218.html 在Android程序开发中,我们经常会去用到Shape这个东西去定义各 ...

  3. Android中使用shape来定义控件

    本文章转接于:http://kofi1122.blog.51cto.com/2815761/521605 Android中常常使用shape来定义控件的一些显示属性,今天看了一些shape的使用,对s ...

  4. android中自定义shape

    <shape> <!-- 实心 --> <solid android:color="#ff9d77"/> <!-- 渐变 --> & ...

  5. Android中使用shape制作一个旋转的progressbar

    public class ZNtestResActivity extends Activity { @Override public void onCreate(Bundle savedInstanc ...

  6. (转)Android中的Shape使用总结

    http://blog.csdn.net/bear_huangzhen/article/details/24488337 在Android程序开发中,我们经常会去用到Shape这个东西去定义各种各样的 ...

  7. Android中有关relativeLayout 和EditText的一些属性

      http://www.cnblogs.com/jqyp/archive/2010/10/23/1859182.html RelativeLayout用到的一些重要的属性: 第一类:属性值为true ...

  8. Android res之shape

    xml控件配置属性 android:background="@drawable/shape" 标签 corners ----------圆角gradient ----------渐 ...

  9. Android 中shape的使用(圆角矩形)

    一.在res/drawable文件夹下创建一个名为gradient_box的xml文件: <?xml version="1.0" encoding="utf-8&q ...

随机推荐

  1. Android ListView初始化简单分析

    下面是分析ListView初始化的源码流程分析,主要是ListVIew.onLayout过程与普通视图的layout过程完全不同,避免流程交代不清楚,以下是一个流程的思维导图. 思维导图是顺序是从左向 ...

  2. Dispatcher及线程操作

    WPF 应用程序启动后,会有两个线程: 1. 一个是用来处理UI呈现(处理UI的请求,比如输入和展现等操作). 2. 一个用来管理 UI的 (对UI元素及整个UI进行管理). WPF在线程里面是不可以 ...

  3. [转]ASP.NET MVC 入门5、View与ViewData

    view在MVC模式中与用户进行最直接的接触,它负责数据的呈现.这里要注意一点就是,view只是负责数据的呈现,所以我们应该要尽量让view中不涉及业务逻辑的处理. 我们来添加一个Blog首页的vie ...

  4. 以后坚持用java

    1.不要贪多,现在专心学习java.读一些jvm的书. 2.研究lucene,hadoop.mahout,和日后用的自然语言分析lingpipe. 3.对于数据挖掘方向,专注与weka的学习,同时注意 ...

  5. WIN7中盾牌的编程-DELPHI

    在PAR文件中引用UAC.RES文件(见下载地址) 代码如下: {$R uac.RES} 点击下载

  6. 【前端】CSS3实现弹出效果

    36氪这个网站上的登录框弹出的时候挺帅气的,想知道它是怎么做的 .. 今天通过问新爷再加上自己琢磨琢磨写出一个小小Demo - 上代码 <!DOCTYPE html> <html&g ...

  7. HW5.5

    import java.util.Scanner; public class Solution { public static void main(String[] args) { Scanner i ...

  8. 【ACM/ICPC2013】POJ基础图论题简析(一)

    前言:昨天contest4的惨败经历让我懂得要想在ACM领域拿到好成绩,必须要真正的下苦功夫,不能再浪了!暑假还有一半,还有时间!今天找了POJ的分类题库,做了简单题目类型中的图论专题,还剩下二分图和 ...

  9. DOCTYPE与浏览器模式详解(标准模式&混杂模式)

    关于渲染模式: 在多年以前(IE6诞生以前),各浏览器都处于各自比较封闭的发展中(基本没有兼容性可谈).随着WEB的发展,兼容性问题的解决越来 越显得迫切,随即,各浏览器厂商发布了按照标准模式(遵循各 ...

  10. 综合而强大的DATASNAP

    从DELPHI2009开始,DATASNAP技术上完全是全新的架构,多层架构不再基于微软的COM,摆脱COM就等于摆脱了WINDOWS的束缚. TCP/IP通信不再需要先开启scktsrvr.exe程 ...