Android按钮按下的时候改变颜色实现方法

转自 https://www.jb51.net/article/45859.htm

  • 首先,在res文件夹下新建一个文件夹drawable,这是无关分辨率的:

  在下面建立一个xml文件:login_button_selector.xml

<selector xmlns:android="http://schemas.android.com/apk/res/android">
        <item android:drawable="@drawable/clr_normal" android:state_pressed="false"/>
        <item android:drawable="@drawable/clr_pressed" android:state_pressed="true"/>

    </selector>
  • 然后在value文件夹下的string.xml文件里添加:
<drawable name="clr_normal">#ff6501</drawable>
 <drawable name="clr_pressed">#a44100</drawable>
  • 最后为button添加:
<Button
  android:layout_marginTop="15dp"
         android:layout_width="match_parent" 
         android:layout_height="wrap_content"
         android:text="@string/loginSubmit"
         android:id="@+id/login"
         android:textColor="@android:color/white"
         android:background="@drawable/login_button_selector"
         />

 

posted on 2019-02-14 11:11  石墨方  阅读(412)  评论(0编辑  收藏  举报

导航