Android——EditText自定义边框、圆角和其常用属性总结

来源:互联网 发布:装完软件蓝屏 编辑:程序博客网 时间:2024/06/05 12:08

看下效果图:


执行步骤:
首先在/res/layout文件夹下创建custom_et_layout.xml布局文件,源代码如下:

[java] view plain copy
 print?
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="fill_parent"  
  4.     android:layout_height="wrap_content" >  
  5.     <EditText  
  6.         android:id="@+id/alertDialog_et"  
  7.         android:background="@drawable/bg_edittext"  
  8.         android:paddingLeft="20dp"  
  9.         android:paddingRight="20dp"  
  10.         android:layout_marginTop="30dp"  
  11.         android:layout_marginLeft="25dp"  
  12.         android:layout_marginRight="30dp"  
  13.         android:layout_marginBottom="30dp"  
  14.         android:textColor="#008A00"  
  15.         android:maxLength="3"  
  16.         android:inputType="number"  
  17.         android:layout_width="match_parent"  
  18.         android:layout_height="wrap_content"  
  19.         android:hint="请输入一个有效整数..."/>  
  20.   
  21. </RelativeLayout>  

属性总结:

---------------------------------------------------------------------------------------------

id: 控件的唯一标识,代码中通过id来找到控件


background: 控件的背景,可以通过该属性来自定义控件不同状态下的外观


padding: 控件中文本内容距离控件边框的距离。单位:dp


paddingLeft: 文本到左边框的距离


paddingRight 文本到有边框的距离


layout_marginTop:margin 指控件之间的距离,top指该控件与它上方控件的间距


layout_marginLeft 该控件与它左侧控件的间距


layout_marginRight 该控件与它右侧控件的间距


layout_marginBottom 该控件与它下方控件的间距


textColor 文本颜色


maxLength 文本最大长度,即字符个数


inputType 输入文本的类型,常用的有number:数字;phone:电话号码;email:电子邮件


layout_width 该控件的宽度


layout_height 该控件的高度


hint: 提示文本内容,在点击后自动消失

--------------------------------------------------------------------------------------------

再加上以下内容的渲染,才能得到一个有边框、圆角的EditText。
步骤:
1.在/res/drawable下创建文件bg_edittext_normal.xml,表示该文本框在常规情况下的样子,内容如下:

[java] view plain copy
 print?
  1. <span style="font-size:18px;"><?xml version="1.0" encoding="utf-8"?>  
  2. <shape xmlns:android="http://schemas.android.com/apk/res/android">  
  3.     <solid android:color="@android:color/transparent" />  
  4.     <corners android:radius="10dip"/>  
  5.     <stroke  
  6.         android:width="1dip"  
  7.         android:color="#BDC7D8" />  
  8. </shape></span>  
2.在/res/drawable下创建文件bg_edittext_focused.xml,表示该文本框在获得焦点情况下的样子,内容如下

[java] view plain copy
 print?
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <shape xmlns:android="http://schemas.android.com/apk/res/android">  
  3.     <solid android:color="@android:color/transparent" />  
  4.     <corners android:radius="10dip"/>  
  5.     <stroke  
  6.         android:width="1dip"  
  7.         android:color="#728ea3" />  
  8. </shape>  

shape中如果不通过Android:shape来指定形状时,默认是矩形,其中solid代表纯色,corners代表角,radius越大,圆角越大,stroke代表边框线


3.在/res/drawable下创建文件bg_edittext.xml,在选择器中应用以上编写的两个样式,内容如下

[java] view plain copy
 print?
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <selector xmlns:android="http://schemas.android.com/apk/res/android">  
  3.     <item  
  4.         android:state_window_focused="false"  
  5.         android:drawable="@drawable/bg_edittext_normal" />  
  6.     <item  
  7.         android:state_focused="true"  
  8.         android:drawable="@drawable/bg_edittext_focused" />  
  9. </selector>  
最后在布局文件的指定控件中的android:background中应用该选择器,例如android:background=“@drawable/bg_edittext”

大功告成!

0 0
原创粉丝点击