随点击来改变按钮的style

    其实这个一直都想要做成一个例子的,因为朋友一直叫我教他,但是一直没能教他,so sorry!希望能帮到他吧!

     这里呢,我只是做了几个按钮,然后就让这个按钮随着自己的点击然后变换按钮颜色,这样就能有一个更好的视觉体验了。

    定义一个xml文件用来写btn的一个style的选择情况:

<?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="#4870ff"/>
            <stroke android:width="2dp" android:color="#ffff00"/>
            <corners android:radius="8dp"/>
            <padding android:bottom="15dp" android:left="10dp" android:right="10dp" android:top="5dp"/>
        </shape>
    </item>
       <item android:state_pressed="false">
        <shape>
            <solid
                 android:color="#4870ff"/>
            <corners android:radius="5dp"/>
             <padding android:bottom="10dp" android:left="10dp" android:right="10dp" android:top="10dp"/>
        </shape>
    </item>
    
    </selector>

 

然后将布局文件的xml写出来:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.btn.MainActivity"
    tools:ignore="MergeRootFrame" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent" >

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@drawable/btn_selector"
            android:text="one" />

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@drawable/btn_selector"
            android:text="two" />

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@drawable/btn_selector"
            android:text="three" />

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@drawable/btn_selector"
            android:text="four" />
    </LinearLayout>

</FrameLayout>

好了,it's ok!看看吧!

转载于:https://www.cnblogs.com/Catherine-Brain/p/3693136.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值