「这是我参与11月更文挑战的第3天,活动详情查看:2021最后一次更文挑战」
👉关于作者
众所周知,人生是一个漫长的流程,不断克服困难,不断反思前进的过程。在这个过程中会产生很多对于人生的质疑和思考,于是我决定将自己的思考,经验和故事全部分享出来,以此寻找共鸣!!!
专注于Android/Unity和各种游戏开发技巧,以及各种资源分享(网站、工具、素材、源码、游戏等)
欢迎关注公众号【空名先生】获取更多资源和交流!
👉前提
这是小空熬夜写的Android新手向系列,欢迎品尝。
上节小空说Button有个重要属性是shape,其实还有个就是selecter。
Selector是背景选择器,主要控制按钮背景的一些变化,比如按下抬起选中状态。
Shape是设置按钮背景的,如圆形,矩形,渐变,圆角等
他们可以单独的用于Button,也可以【selector】内嵌【shape】,实现更多的按钮效果。
他们的优点就是我们自己可以很方便的自定义背景,相对于使用图片来说,可以减少安装包大小,而且能更好的在不同的设备上不失真。
缺点嘛,就是你的开发时间长了,毕竟之前是直接用图片现在成了用代码来实现。看实际开发情况,选择用图片还是自定义shape。
👉实践过程
今天,我们先说shape。
在【res-drawable】右键创建个Drawable Resource File ,弹出框写文件名创建文件,设置默认【Root element】为shape。
我们来看看shape内部第一层都有什么属性:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid></solid>
<gradient></gradient>
<corners></corners>
<padding></padding>
<size></size>
<stroke></stroke>
</shape>
从名字我们就能大致猜出意思了:
corners:此属性是来定义四边圆角的。
gradient:是用来定义渐变颜色的,和solid不同时出现。
solid:是用来绘制实心内部填充色的,和gradient不同时出现。
stroke:描边效果,边缘实线虚线颜色等。
size和padding:何止大小和内边距,padding多数情况下不使用。
示例如下:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/white"
tools:context=".TextActivity">
<!--圆形-->
<Button
android:id="@+id/mtBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="50dp"
android:layout_marginTop="10dp"
android:background="@drawable/btn_shape"
android:elevation="10dp"
android:rotationX="-23"
android:rotationY="-31"
android:rotationZ="-31"
android:text="这是按钮"
android:translationX="2dp"
android:translationZ="10dp" />
<!--空心圆形背景-->
<Button
android:layout_width="80dp"
android:layout_height="80dp"
android:layout_marginLeft="50dp"
android:layout_marginTop="70dp"
android:background="@drawable/btn_shape0"
android:elevation="10dp"
android:text="芝麻粒儿"
android:translationX="2dp"
android:translationZ="10dp" />
<!-- 这是圆角按钮-->
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="50dp"
android:layout_marginTop="150dp"
android:background="@drawable/btn_shape1"
android:elevation="10dp"
android:rotationX="-10"
android:rotationY="20"
android:rotationZ="-30"
android:text="空名先生"
android:translationX="2dp"
android:translationZ="10dp" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:elevation="30dp"
android:text="this button"
android:textAllCaps="false" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="30dp"
android:padding="6dp"
android:layout_marginTop="220dp"
android:autoLink="web"
android:background="@drawable/btn_shape2"
android:gravity="center"
android:text="文本:知乎-掘金"
android:textColor="#ffffff"
android:textSize="16sp" />
</RelativeLayout>
btn_shape0.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:innerRadius="30dp"
android:shape="ring"
android:thickness="2dp"
android:useLevel="false">
<solid android:color="#ff0000" />
</shape>
btn_shape1.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="#00ff00" />
<corners android:radius="12dp" />
</shape>
btn_shape2.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<corners android:radius="50dp" />
<gradient
android:angle="0"
android:centerColor="#0000ff"
android:endColor="#00ff00"
android:startColor="#ff0000"/>
</shape>
👉其他
📢作者:小空和小芝中的小空
📢转载说明-务必注明来源:芝麻粒儿 的个人主页 - 专栏 - 掘金 (juejin.cn)
📢欢迎点赞👍收藏🌟留言📝