Android之实现TextView控件圆角以及Button点击、焦点效果

默认情况下,TextView是不带边框的,如果想要为TextView添加边框,只能考虑为TextView设置一个背景Drawable。在Drawable文件夹下新建一个layout类型的xml文件,然后将Root tag改成shape,这样xml文件也可以当Drawable使用。
bg_border.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <solid android:color="#0000"></solid>
    <stroke android:width="4px" android:color="#f00"></stroke>
    <corners android:topLeftRadius="20dp"
        android:topRightRadius="20dp"
        android:bottomLeftRadius="20dp"
        android:bottomRightRadius="20dp"></corners>
</shape>
最后再把TextView的android:background属性设置成自定义的xml样式文件就可以了:
android:background="@drawable/bg_border"
另外还可以设置渐变色、填充色等,如下:
gradient   -- 颜色渐变
        startcolor  起点颜色
        endcolor  终点颜色
        android:angle 角度  0是从左到右,90是从下到上
solid          --  填充
stroke        --  描边 
corners      --  圆角 

padding     -- 内容离边界的距离

同理,设置Button在不同状态下的背景,也是使用在Drawable文件夹下添加xml文件,不过资源类型改为selector。

selector.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/temp1" />
 <item android:state_pressed="false" android:state_focused="false"
  android:drawable="@drawable/temp2" />
 <item android:state_focused="true" android:drawable="@drawable/temp3" />
 <item android:state_focused="false" android:drawable="@drawable/temp4" />
</selector>

然后在button中设置如下:

<Button
 android:background="@drawable/selector"/>

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值