给android中按钮美美容
来源:互联网 发布:社工库源码 编辑:程序博客网 时间:2024/04/30 05:19
android中默认的按钮一般是灰色,比较丑,而且按钮多了会显得很单调,那我们如何给按钮美美容呢?
一.原理:在xml中定义出想要的形状和颜色,当点击,选中,默认或不可触发的这些状态分别对应着不同的xml文件,然后在按钮的创建中引用,从而产生美容的效果。
如下图所示,图1为默认效果,图2为点击效果
图1
图2
二.代码:在drawable-hdpi新建xml文件bt_orange_default.xml代表按钮默认时的状态内容为:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape android:shape="rectangle">
<solid android:color="#FFEC7600" />
<corners
android:topLeftRadius="15dip"
android:topRightRadius="15dip"
android:bottomLeftRadius="15dip"
android:bottomRightRadius="15dip" />
</shape>
</item>
<item android:top="2px" android:bottom="2px" android:left="2px" android:right="2px">
<shape>
<gradient
android:startColor="#FFEC7600" android:endColor="#FFFED69E"
android:type="linear" android:angle="90"
android:centerX="1" android:centerY="1" />
<corners
android:topLeftRadius="15dip"
android:topRightRadius="15dip"
android:bottomLeftRadius="15dip"
android:bottomRightRadius="15dip" />
</shape>
</item>
</layer-list>
将这个文件在同一目录再新建3个xml文件命名为bt_orange_selected.xml,bt_orange_pressed.xml,bt_orange_enabled.xml,分别带表选中,点击,不可触发的三种状态。
这些文件内容可以根据需求修改,也可以和bt_orange_default.xml一样。笔者列出自己的修改内容,仅供参考。
bt_orange_selected.xml内容为:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape android:shape="rectangle">
<solid android:color="#FFEC7600" />
<corners
android:topLeftRadius="5dip"
android:topRightRadius="5dip"
android:bottomLeftRadius="5dip"
android:bottomRightRadius="5dip" />
</shape>
</item>
<item android:top="1px" android:bottom="1px" android:left="1px" android:right="1px">
<shape>
<gradient
android:startColor="#FFED7600" android:endColor="#FFFFD69E"
android:type="linear" android:angle="90"
android:centerX="0.5" android:centerY="0.5" />
<corners
android:topLeftRadius="5dip"
android:topRightRadius="5dip"
android:bottomLeftRadius="5dip"
android:bottomRightRadius="5dip" />
</shape>
</item>
</layer-list>
bt_orange_enabled.xml内容为:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape android:shape="rectangle">
<solid android:color="#FFEC7600" />
<corners
android:topLeftRadius="5dip"
android:topRightRadius="5dip"
android:bottomLeftRadius="5dip"
android:bottomRightRadius="5dip" />
</shape>
</item>
<item android:top="1px" android:bottom="1px" android:left="1px" android:right="1px">
<shape>
<gradient
android:startColor="#FFEDFFFF" android:endColor="#FFFDEFFF"
android:type="linear" android:angle="90"
android:centerX="0.5" android:centerY="0.5" />
<corners
android:topLeftRadius="5dip"
android:topRightRadius="5dip"
android:bottomLeftRadius="5dip"
android:bottomRightRadius="5dip" />
</shape>
</item>
bt_orange_pressed.xml内容为:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape android:shape="rectangle">
<solid android:color="#FFEC7600" />
<corners
android:topLeftRadius="5dip"
android:topRightRadius="5dip"
android:bottomLeftRadius="5dip"
android:bottomRightRadius="5dip" />
</shape>
</item>
<item android:top="1px" android:bottom="1px" android:left="1px" android:right="1px">
<shape>
<gradient
android:startColor="#FFEDA600" android:endColor="#FFFFB69E"
android:type="linear" android:angle="90"
android:centerX="0.5" android:centerY="0.5" />
<corners
android:topLeftRadius="5dip"
android:topRightRadius="5dip"
android:bottomLeftRadius="5dip"
android:bottomRightRadius="5dip" />
</shape>
</item>
</layer-list>
接着在这个目录下建立bt_orange.xml,其内容为:
<?xml version="1.0" encoding="UTF-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true"
android:drawable="@drawable/bt_orange_pressed" />
<item android:state_focused="true"
android:drawable="@drawable/bt_orange_selected" />
<item android:state_enabled="false"
android:drawable="@drawable/bt_orange_enabled" />
<item android:drawable="@drawable/bt_orange_default" />
</selector>
最后在布局文件中找到要修改的按钮,更改按钮属性为
<Button
android:id="@+id/button2"
android:layout_width="139dp"
android:layout_height="139dp"
android:layout_x="254dp"
android:layout_y="421dp"
android:background="@drawable/bt_orange"
android:text="这个按钮到底漂亮"
android:textSize="20sp" />
- 给android中按钮美美容
- android 中给按钮注册事件
- 给Android按钮添加图标
- Android 给按钮绑定事件
- 美容
- 美容
- Android EditText中通过drawabeRight等添加了清空按钮 如何给按钮添加点击事件?
- android中给按钮注册监听器的一种新颖的方法
- Android中给Button加上selector——点击按钮后变成不一样的图片
- Android 在xml布局配置文件中给Button按钮添加事件
- Android中给Button加上selector——点击按钮后变成不一样的图片
- Android中给按钮同时设置背景和圆角示例代码
- Android给按钮添加单击事件
- 给Android中的按钮加图片
- 给Android标题栏加上返回按钮
- QT中如何给表格添加按钮
- MFC中给按钮添加图片
- javaswing中给按钮添加快捷键
- Linux系统下ifconfig命令使用及结果分析
- 多线程的那些事(3)JAVA多线程设计
- 优秀开源代码解读:JS与iOS Native Code互调实现方法
- hdu 2642 Stars
- Cocos2d-x开发网络游戏(四) 处理网络线程
- 给android中按钮美美容
- 大学英语四六级13年12月大改革应对办法全套复习规划
- 如何查看chrome下载内容
- MongoDb Samus c# Find函数的使用说明
- Redhat服务器利用rsync和inotify-tools实现实时增量备份的方法(详解)
- hibernate 方言
- Oracle RAC 日常维护
- 麻省理工学院公开课:信号与系统:模拟与数字信号处理> 调幅演示
- JAVA+FlexPaper+OpenOffice+SWFTools文档预览