Android自定义背景的设置方法
Android自定义背景的设置方法
本文主要介绍最基本的控件Button、TextView、EditText的自定义背景设置方法。
比如下面的图片就用到了自定义的背景:
一.去掉背景色
android:background="@null"
能去掉EditText下面的下划线,也能去掉Button的背景色。
二.自定义背景的使用方法
(一)在res文件夹下的drawable-hdpi文件夹里面创建一个设置背景的
资源文件shap1.xml
如图1:
如图2:
如图3:
(二)在shap1.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="1dip" />
<stroke
android:width="2px"
android:color="#ccC71485" />
shape>
上面的代码中按钮的四个角的弧度可以是不同的,
比如:
android:topLeftRadius=" "
android:topRightRadius=" "
android:bottomLeftRadius="" android:bottomRightRadius=""
上面四个属性可以随意设置四个角度的弧度大小
(三)调用文件
文件写好后可以直接当做背景资源利用,如:
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/shap1" //通过文件名直接调用写好的文件资源
android:text="登 录" />
总结:其实上面就是把写好的文件资源当做一个背景图片来使用。
三.上面图片的程序代码:
(一)两个背景颜色资源文件
1.登录按钮的背景设置文件shap1.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
<solid android:color="#ccC71485" />
<corners android:radius="5dp" />
shape>
2.手机注册的按钮的背景设置文件shap2.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="1dip" />
<stroke
android:width="2px"
android:color="#ccC71485" />
shape>
(二)布局文件
<?xml version="1.0" encoding="utf-8"?>
效果图:
四.一些其他的知识
比如输入框外面添加一个边框就是背景资源文件的使用,
下面这个输入框只设置了外边框的颜色,角度都不用设置。
五.Drawable资源文件的其他知识
背景颜色还可以产生渐变的效果
资源文件代码如下:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
<corners android:radius="10dip" />
<gradient
android:startColor="#f00"
android:centerColor="#0f0"
android:endColor="#00f"
/>
shape>
效果如下:
背景资源的用法基本上就上面这几个了,学会使用能过提高界面的美观效果。
更多相关文章
- android第一天学习基本配置与安装过程和启动
- 六、ANDROID资源文件
- Android(安卓)DiskLruCache完全解析,硬盘缓存的最佳方案
- 新浪微博SDK在Eclipse引入
- Android中OkHttp的使用
- 处女男学Android(十三)---Android(安卓)轻量级数据存储之SharedPre
- Unity与Android通信
- Adapter那点事
- 代码在android 8 上面正常运行在 android 9 上面却不能联网问题