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

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

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

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

paddingLeft: 文本到左边框的距离

paddingRight 文本到有边框的距离

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

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

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

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

textColor 文本颜色

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

扫描二维码关注公众号,回复: 460253 查看本文章

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

layout_width 该控件的宽度

layout_height 该控件的高度

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

=========================================================================================

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

    <?xml version="1.0" encoding="utf-8"?>  
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
        android:layout_width="fill_parent"  
        android:layout_height="wrap_content" >  
        <EditText  
            android:id="@+id/alertDialog_et"  
            android:background="@drawable/bg_edittext"  
            android:paddingLeft="20dp"  
            android:paddingRight="20dp"  
            android:layout_marginTop="30dp"  
            android:layout_marginLeft="25dp"  
            android:layout_marginRight="30dp"  
            android:layout_marginBottom="30dp"  
            android:textColor="#008A00"  
            android:maxLength="3"  
            android:inputType="number"  
            android:layout_width="match_parent"  
            android:layout_height="wrap_content"  
            android:hint="请输入一个有效整数..."/>  

    </RelativeLayout>  

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

<?xml version="1.0" encoding="utf-8"?>  
<shape xmlns:android="http://schemas.android.com/apk/res/android">  
    <solid android:color="@android:color/transparent" />  
    <corners android:radius="10dip"/>  
    <stroke  
        android:width="1dip"  
        android:color="#BDC7D8" />  
</shape>

2.在/res/drawable下创建文件bg_edittext_focused.xml,表示该文本框在获得焦点情况下的样子,内容如下

    <?xml version="1.0" encoding="utf-8"?>  
    <shape xmlns:android="http://schemas.android.com/apk/res/android">  
        <solid android:color="@android:color/transparent" />  
        <corners android:radius="10dip"/>  
        <stroke  
            android:width="1dip"  
            android:color="#728ea3" />  
    </shape>  

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

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

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

最后在布局文件的指定控件中的android:background中应用该选择器,例如android:background=“@drawable/bg_edittext”

结束 效果图
这里写图片描述

转载自 http://blog.csdn.net/xuejingfu1/article/details/51597032

猜你喜欢

转载自blog.csdn.net/qq_33495943/article/details/79083048