Button 自定义(一)-shape

需求:自定义Button,使用系统自定义Shape;

效果图:

1.默认状态

2.选中状态

 

实现分析:

1.目录结构:

 

代码实现:

1.button_normal.xml

复制代码
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >

    <solid android:color="#ff007aff" />

    <corners
        android:bottomLeftRadius="6dp"
        android:bottomRightRadius="6dp"
        android:topLeftRadius="6dp"
        android:topRightRadius="6dp" />

</shape>
复制代码

2.button_select.xml

复制代码
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
    <solid android:color="#FFD700" />

    <corners
        android:bottomLeftRadius="6dp"
        android:bottomRightRadius="6dp"
        android:topLeftRadius="6dp"
        android:topRightRadius="6dp" />

</shape>
复制代码

solid:实心,就是填充的意思

corners:圆角
android:radius为角的弧度,值越大角越圆。

 

3.button_selector.xml

复制代码
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:drawable="@drawable/button_select" android:state_focused="true"></item>
    <item android:drawable="@drawable/button_select" android:state_pressed="true"></item>
    <item android:drawable="@drawable/button_normal" android:state_focused="false" android:state_pressed="false"></item>
    <item android:drawable="@drawable/button_normal" ></item>

</selector>
复制代码

4.fragment_main.xml

复制代码
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.jjc.demo.MainActivity$PlaceholderFragment" >

    <Button
        android:id="@+id/button_test"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/button_selector"
        android:text="推荐组合"
        android:textColor="@android:color/white" />

</RelativeLayout>
复制代码

 

 

代码:http://pan.baidu.com/s/1ntBN5Df

posted @   似水流云  阅读(638)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
阅读排行:
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 零经验选手,Compose 一天开发一款小游戏!
· 因为Apifox不支持离线,我果断选择了Apipost!
· 通过 API 将Deepseek响应流式内容输出到前端
点击右上角即可分享
微信分享提示