•普通Button

<?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:gravity="center"
android:padding="10dp"> <Button
android:id="@+id/btn_1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#D52BD5"
android:text="普通Button"
android:textColor="#000000"
android:textSize="20sp"
/> </RelativeLayout>

•运行效果

  

•圆角 Button

  点击 app/src/main/res 找到 drawable 文件夹,右击->New->Drawable Resource File。

  

  新建一个文件名为 round_corner 根元素为 shape 的 .xml 文件,添加如下代码:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle"> <corners android:radius="20dp" /> <solid android:color="#D52BD5" /> </shape>
  • solrd : 填充

    • color : 设置填充颜色
  • corners : 设置圆角

    • radius : 设置四个角的弯曲度
    • topLeftRadius : 设置左上角的弯曲度

    • topRightRadius : 设置右上角的弯曲度

    • bottomLeftRadius : 设置左下角的弯曲度

    • bottomRightRadius :设置右下角的弯曲度

  配置好 round_corner.xml 文件后,只需更改一下普通 Button 中的 background 属性即可实现圆角;

     android:background="@drawable/round_corner"

  代码如下:

<?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:gravity="center"
android:padding="10dp"> <Button
android:id="@+id/btn_1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/round_corner"
android:text="圆角Button"
android:textColor="#000000"
android:textSize="20sp" /> </RelativeLayout>

•运行效果

  

•描边 Button

  点击 app/src/main/res 找到 drawable 文件夹,右击->New->Drawable Resource File。

  

  新建一个 stroke.xml 文件,在该文件中加入如下代码:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle"> <corners android:radius="20dp" /> <stroke
android:width="2dp"
android:color="#22DD22" /> </shape>
  • stroke : 描边

    • width : 设置描边的宽度
    • color : 设置描边的颜色

  配置好 stroke.xml 文件后,将 Button 中的 background 属性更改一下即可实现描边。

     android:background="@drawable/stroke"

  代码如下:

<?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:gravity="center"
android:padding="10dp"> <Button
android:id="@+id/btn_stroke"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/stroke"
android:text="描边 Button"
android:textColor="#000000"
android:textSize="20sp" /> </RelativeLayout>

•运行效果

  

•按压 Button

点击 app/src/main/res 找到 drawable 文件夹,右击->New->Drawable Resource File。

  

  新建一个 press_effect.xml 文件,在该文件中加入如下代码:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android"> <item
android:state_pressed="false"
android:drawable="@color/teal_200"/> <item
android:state_pressed="true"
android:drawable="@color/teal_700" />
</selector>

  需要注意的是, android:drawable = "这里如果使用RGB颜色代码(#CC56CC)"  会报错:

AAPT: error: '#CC56CC' is incompatible with attribute drawable (attr) reference.

  解决方案就是,将你需要的颜色代码放入到 res/values/colors.xml 中,通过 @color/XXX 来使用这些颜色。

  配置好 press_effect.xml 文件后,将 Button 中的 background 属性更改一下即可实现按压效果。

     android:background="@drawable/press_effect"     

  代码如下:

<?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:gravity="center"
android:padding="10dp"> <Button
android:id="@+id/btn_press_effect"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/press_effect"
android:text="描边 Button"
android:textColor="#000000"
android:textSize="20sp" /> </RelativeLayout>

•运行效果

  

•圆角按压效果

  <item> 标签中也可以放入 <shape> 标签,这样就使得样式更加丰富。

  更改 press_effect.xml 代码如下:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:state_pressed="false">
<shape>
<corners android:radius="10dp" />
<solid android:color="@color/teal_200" />
</shape>
</item> <item>
<shape>
<corners android:radius="10dp" />
<solid android:color="@color/teal_700" />
</shape>
</item> </selector>

•运行效果

  

  与上一个效果对比,圆角更加美观。

Android Studio 之 Button(圆角,描边,按压效果)的更多相关文章

  1. Android Studio中Button等控件的Text中字符串默认大写的解决方法

    初学Android的时候,在Android Studio中xml里面添加一个Button.EditText等控件后,它的Text总是会显示大写,即使你输入的字符串是小写也不行,控制字符串大小写的属性是 ...

  2. Android Studio 制作一个循环播报的效果

    这个就是用到了一个TextView 控件,直接上代码. <TextView android:id="@+id/tv_7" android:layout_width=" ...

  3. Android拓展系列(10)--使用Android Studio阅读整个Android源码

    之前一直在windows下用source insight阅读android源码,效果非常好.后来远程异地服务器,网络限制,一直用ssh + vim,现在主要还是以这种方式.最近发现一个不错的东西(早就 ...

  4. Android Studio 使用感受 错误解决

    刚到公司不久,公司叫我用Android studio软件,曾经一直在用的是eclipse ADT.突然接触到的新名词让我有点适应只是来. 好吧,既然是公司要求,肯定有它的道理.就从网上下载了它的安装包 ...

  5. Button的几种常用的xml背景,扁平化,下划线,边框包裹,以及按压效果

    Button的几种常用的xml背景,扁平化,下划线,边框包裹,以及按压效果 分享下我项目中用到的几种Button的效果,说实话,还真挺好看的 一.标准圆角 效果是这样的 他的实现很简单,我们只需要两个 ...

  6. Android 自己定义UI圆角button

    Android实际开发中我们一般须要圆角的button,普通情况下我们能够让美工做出来对应的button图片.然后放上去就可以,另外我们能够在布局文件里直接设置,也能够达到一样的效果. 以下解说在布局 ...

  7. Android中实现ListView圆角效果[转]

    本文演示如何Android中实现ListView圆角效果. 无论是网站,还是APP,人们都爱看一些新颖的视图效果.直角看多了,就想看看圆角,这几年刮起了一阵阵的圆角设计风:CSS新标准纳入圆角元素,特 ...

  8. Android学习之——实现圆角Button

    在drawable文件夹下新建btn_shape.xml文件: <?xml version="1.0" encoding="utf-8"?> < ...

  9. android selector设置button点击效果(具体)以及常见问题

    button的点击效果学习起来其实比較easy,此点对开发人员来说也是使用的比較频繁的一个知识点,与它相关的还有编辑框的获取焦点时改变背景颜色.选择button选择时改变字体颜色等等.这些其实都是用到 ...

随机推荐

  1. how to tell a function arguments length in js

    how to tell a function arguments length in js JavaScript函数不对参数值(参数)执行任何检查 https://www.w3schools.com/ ...

  2. taro external-class

    taro external-class https://nervjs.github.io/taro/docs/component-style.html externalClasses child co ...

  3. 以代码为剑、数学为犁,SPC构建NGK算力生态体系

    人类创造工具,工具反过来也改变着人类.以区块链为核心的货币革命率先吹响了对金融世界重塑的号角.以代码为剑.数学为犁,区块链构建了新的网路信任体系,这是一切的开始.基于此,NGK区块链技术将赋能实体产业 ...

  4. iOS 兼容性处理

    1. scroll滑动层,在iOS中滑动不流畅的处理 -webkit-overflow-scrolling:touch; //在滑动层标签添加这个样式 2. iOS 系统中input标签,去掉圆角效果 ...

  5. eclipse中将项目加载到tocat报错:Tomcat version 6.0 only supports J2EE 1.2, 1.3, 1.4, and Java EE 5 Web modules

    第一种解决方法:只需要找到导入项目的配置文件即可,举个栗子:D:\公司\iptv_gx\iptv_gx\.settings\org.eclipse.wst.common.project.facet.c ...

  6. ElasticSearch7.9.2设置密码

    1:设置ElasticSearch的密码 1.1:停止运行ElasticSearch,修改配置. vim elasticsearch-7.9.2/config/elasticsearch.yml 新增 ...

  7. WPF -- 自定义按钮

    本文介绍WPF一种自定义按钮的方法. 实现效果 使用图片做按钮背景: 自定义鼠标进入时效果: 自定义按压效果: 自定义禁用效果 实现效果如下图所示: 实现步骤 创建CustomButton.cs,继承 ...

  8. Linux系统编程【4】——文件系统

    pwd命令的作用 Linux的文件系统比较庞大,所以笔者从pwd这一命令入手,在实现的过程中加深对文件系统的了解. 输入:man pwd 从指导文档中可以看到,pwd命令的作用是显示出当前所处位置,以 ...

  9. 微信小程序滚动条设置

    隐藏滚动条 ::-webkit-scrollbar { width: 0rpx; height: 0rpx; background-color: transparent; } 其实设置为宽高为 0 或 ...

  10. 后端程序员之路 45、nginx CORS 跨域

    在提供api给其它应用使用时,有时我们会要限制它的跨域使用,而有时,我们又要用CORS来打破AJAX只能同源使用的限制 跨域资源共享 CORS 详解 - 阮一峰的网络日志http://www.ruan ...