Android之EditText
2014-03-17 11:18
399 查看
一、为EditText添加图片
在EditText中添加图片,只需要设置android:drawableLeft、android:drawableRight、android:drawableTop或者android:drawableBottom属性,在xml中即可完成。例如:
[html] view
plaincopyprint?
<span style="font-size:16px;"> <EditText
android:id="@+id/text"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="hello"
android:drawableLeft="@drawable/search"
/></span>
效果如下:
二、为EditText添加Button
在EditText中添加Button相对麻烦一些,这里实现的方法是利用相对布局RelativeLayout,关键在于设置android:layout_alignBaseline(alignRight、alignLeft、alignTop、alignBottom)属性使Button控件的边缘与EditText的边缘对齐。
[html] view
plaincopyprint?
<span style="font-size:18px;"> <RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
<EditText
android:id="@+id/searcheidt"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:singleLine="true"
android:hint="search"
/>
<Button
android:id="@+id/search_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/search"
android:layout_alignTop="@id/searcheidt"
android:layout_alignRight="@id/searcheidt"
android:layout_marginRight="5px"
/>
</RelativeLayout> </span>
这里我设置了android:layout_alignTop和 android:layout_alignRight属性,是Button的顶部边缘和右边缘分别于EditText的顶部边缘和右边缘对齐,即可实现(此时搜索图标表示一个按钮,通过设置监听器可以实现相应功能):
三、shape的使用
在android中常常用shape来修改控件的显示属性,比如:圆角、描边之类的。首先,写一个xml文件:edittext_shape.xml
[html] view
plaincopyprint?
<span style="font-size:18px;"><?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<!--圆角-->
<corners
android:radius="5dp"/>
<!--实心-->
<solid
android:color="#eeeeee"/>
<!--描边-->
<stroke
android:width="1dip"
android:color="#c6cbce"/>
<!--渐变-->
<gradient/>
</shape></span>
把以上edittext_shape.xml文件放到drawable文件夹内,然后只需要在设置EditText的background属性即可:
android:background="@drawable/edittext_shape"
如下:
[html] view
plaincopyprint?
<span style="font-size:18px;"><EditText
android:id="@+id/text"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="hello"
android:drawableLeft="@drawable/search"
android:background="@drawable/edittext_shape"
/></span>
效果如下:
在EditText中添加图片,只需要设置android:drawableLeft、android:drawableRight、android:drawableTop或者android:drawableBottom属性,在xml中即可完成。例如:
[html] view
plaincopyprint?
<span style="font-size:16px;"> <EditText
android:id="@+id/text"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="hello"
android:drawableLeft="@drawable/search"
/></span>
效果如下:
二、为EditText添加Button
在EditText中添加Button相对麻烦一些,这里实现的方法是利用相对布局RelativeLayout,关键在于设置android:layout_alignBaseline(alignRight、alignLeft、alignTop、alignBottom)属性使Button控件的边缘与EditText的边缘对齐。
[html] view
plaincopyprint?
<span style="font-size:18px;"> <RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
<EditText
android:id="@+id/searcheidt"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:singleLine="true"
android:hint="search"
/>
<Button
android:id="@+id/search_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/search"
android:layout_alignTop="@id/searcheidt"
android:layout_alignRight="@id/searcheidt"
android:layout_marginRight="5px"
/>
</RelativeLayout> </span>
这里我设置了android:layout_alignTop和 android:layout_alignRight属性,是Button的顶部边缘和右边缘分别于EditText的顶部边缘和右边缘对齐,即可实现(此时搜索图标表示一个按钮,通过设置监听器可以实现相应功能):
三、shape的使用
在android中常常用shape来修改控件的显示属性,比如:圆角、描边之类的。首先,写一个xml文件:edittext_shape.xml
[html] view
plaincopyprint?
<span style="font-size:18px;"><?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<!--圆角-->
<corners
android:radius="5dp"/>
<!--实心-->
<solid
android:color="#eeeeee"/>
<!--描边-->
<stroke
android:width="1dip"
android:color="#c6cbce"/>
<!--渐变-->
<gradient/>
</shape></span>
把以上edittext_shape.xml文件放到drawable文件夹内,然后只需要在设置EditText的background属性即可:
android:background="@drawable/edittext_shape"
如下:
[html] view
plaincopyprint?
<span style="font-size:18px;"><EditText
android:id="@+id/text"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="hello"
android:drawableLeft="@drawable/search"
android:background="@drawable/edittext_shape"
/></span>
效果如下:
相关文章推荐
- Android进阶练习 - 高效显示Bitmap(管理Bitmap内存)
- 通过联系人号码获取联系人的姓名(android开发)
- Android生命周期
- Android 颜色Color(转)
- Android layout属性详细说明
- Android 全局Activity动画设置
- Android中一些有用的小方法(不定时更新)
- android按键映射
- android 电话状态的监听(来电和去电)
- Android判断是否联网
- Android部分开源项目源码分析--log信息的打印
- Android高级图片滚动控件,编写3D版的图片轮播器
- [转]Android的Handler总结
- (opencv)Android人脸识别
- Android中更详细的log获取方法
- Android——Hardware Acceleration
- Android各种单位转换&&Android的View用到的部分代码
- apis.view 之 Animation,AutoComplete,Baseline,Chronometer
- android Phonegap项目的HelloWorld
- android 自定义dialog中去除黑色背景 WindowManager