Android如何自定义按钮效果
作者:qq_32198277 时间:2022-08-14 13:11:53
安卓原生的按钮是多么丑,效果是多么单调,大家也是有目共睹的。
要做一个APP少不了使用按钮,一个好看的按钮少不了好看的效果和外表,这次主要跟大家讲讲如何用drawable的xml文件弄一些好看的自定义样式。
首先是外表
在APP中四四方方,灰色底黑色字的按钮是很难看的,我们希望看到的是圆角,彩色,白字(根据你的个人审美也可以是其他样式)。
首先是在layout里新建一个按钮 ,然后在drawable文件夹里新建一个drawable resource file ,不妨起名为shape ,加一个selector节点里新建一个item节点,在item节点里添加样式
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape>
<!-- 填充的颜色(亮绿) -->
<solid android:color="#28baa7" />
<!-- 设置按钮的四个角为弧形 -->
<!-- android:radius 弧形的半径 -->
<corners android:radius="5dip" />
<!-- padding:Button里面的文字与Button边界的间隔 -->
<padding
android:left="10dp"
android:top="10dp"
android:right="10dp"
android:bottom="10dp"
/>
</shape>
</item>
</selector>
如果想做点击后按钮的样式则要 再添加一个item节点,整个的drawable就是这样
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true">
<shape>
<!-- 填充的颜色(白色) -->
<solid android:color="#aaa" />
<!-- 设置按钮的四个角为弧形 -->
<!-- android:radius 弧形的半径 -->
<corners android:radius="5dip" />
<!-- padding:Button里面的文字与Button边界的间隔 -->
<padding
android:left="10dp"
android:top="10dp"
android:right="10dp"
android:bottom="10dp"
/>
</shape>
</item>
<item>
<shape>
<!-- 填充的颜色(亮绿) -->
<solid android:color="#28baa7" />
<!-- 设置按钮的四个角为弧形 -->
<!-- android:radius 弧形的半径 -->
<corners android:radius="5dip" />
<!-- padding:Button里面的文字与Button边界的间隔 -->
<padding
android:left="10dp"
android:top="10dp"
android:right="10dp"
android:bottom="10dp"
/>
</shape>
</item>
</selector>
这样的样式是一般时是亮绿色四周圆角,点击时是白色圆角,除了点击之外还有很多的情况请用时自行查找,也是大同小异
好啦,接下来是布局文件的设置
<Button
android:layout_width="150dp"
android:layout_height="wrap_content"
android:text="button"
android:textColor="#fff"
android:background="@drawable/shape"
android:id="@+id/btn"
android:layout_gravity="center_horizontal" />
这个把文字设为白色居中,好啦,一个比较好看的按钮就这么诞生啦
标签:Android,自定义,按钮
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
C语言使用strcmp()函数比较两个字符串的实现
2023-10-15 13:06:55
Spring Boot 多数据源处理事务的思路详解
2022-04-21 18:21:47
C#验证身份证的函数
2022-06-16 04:49:37
C#9.0推出的4个新特性介绍
2021-10-10 07:49:29
使用Java实现三种等级的扫雷游戏(完整版)
2023-05-10 07:34:17
![](https://img.aspxhome.com/file/2023/8/83028_0s.png)
Mybatis中如何进行批量更新(updateBatch)
2022-10-11 13:42:00
详解如何在Spring Security中自定义权限表达式
2023-11-08 05:11:24
![](https://img.aspxhome.com/file/2023/6/58676_0s.jpg)
44条Java代码优化建议
2023-12-22 06:03:49
![](https://img.aspxhome.com/file/2023/6/62776_0s.png)
java 对象的克隆(浅克隆和深克隆)
2021-06-27 20:43:20
Spring Cloud Alibaba使用Nacos作为注册中心和配置中心
2021-07-15 18:18:42
![](https://img.aspxhome.com/file/2023/3/67983_0s.png)
Spring实战之方法级别缓存用法示例
2022-10-20 02:00:32
基于Spring p标签和c标签注入方式
2023-11-25 22:26:36
![](https://img.aspxhome.com/file/2023/7/109947_0s.png)
SpringBoot自定义MessageConvert详细讲解
2023-04-23 19:24:39
![](https://img.aspxhome.com/file/2023/9/129129_0s.png)
SpringBoot入坑笔记之spring-boot-starter-web 配置文件的使用
2021-12-15 13:11:46
![](https://img.aspxhome.com/file/2023/0/78370_0s.png)
深入理解Java之HashMap源码剖析
2022-03-24 12:18:06
![](https://img.aspxhome.com/file/2023/2/122292_0s.jpg)
java生成json数据示例
2023-10-30 04:06:45
![](https://img.aspxhome.com/file/2023/9/64059_0s.jpg)
Redis6搭建集群并在SpringBoot中使用RedisTemplate的实现
2023-10-31 14:48:05
![](https://img.aspxhome.com/file/2023/2/58622_0s.png)
Android SeekBar在刷新使用中需要注意的问题
2022-08-14 17:55:59
![](https://img.aspxhome.com/file/2023/3/114363_0s.jpg)
解决@RequestBody部分属性丢失的问题
2023-08-01 15:00:21
![](https://img.aspxhome.com/file/2023/1/63171_0s.png)
Android Studio 超级简单的打包生成apk的方法
2023-08-07 18:57:28
![](https://img.aspxhome.com/file/2023/6/118926_0s.png)