![](https://i-blog.csdnimg.cn/blog_migrate/f0dbadd44743fba416db84bbe2749da8.jpeg)
![](https://i-blog.csdnimg.cn/blog_migrate/e3080ec6fef556e41201252ce221e933.jpeg)
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"
tools:context=".MainActivity">
<Button
android:id="@+id/btn_login"
android:layout_width="270dp"
android:layout_height="44dp"
android:background="@drawable/selector_btn_login"
android:enabled="true"
android:text="登 录"
android:textColor="#ffffffff"
android:textSize="16sp"
android:layout_centerInParent="true"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="测试"
android:visibility="visible"
android:layout_centerHorizontal="true"
android:layout_below="@id/btn_login"
android:onClick="changeLoginBtn"
/>
</RelativeLayout>
- 背景
selector_btn_login.xml
代码:
<?xml version="1.0" encoding="utf-8"?>
<selector
xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_enabled="false" android:drawable="@drawable/shape_login_inactive"/>
<item android:state_enabled="true" android:drawable="@drawable/shape_login_active"/>
</selector>
- 高亮状态
shape_login_active.xml
代码:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="#00391F" />
<corners android:radius="25dp" />
</shape>
- 置灰状态
shape_login_inactive.xml
代码:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
<solid android:color="#7F9C8F" />
<corners android:radius="25dp" />
</shape>
- 变色控制:
btnLogin.setEnabled(!btnLogin.isEnabled());