Android中Button的形状及背景自定义

效果如下:

按钮

具体实现如下:
文件名为:guide_go_button.xml

<?xml version="1.0" encoding="utf-8"?>
<selector
  xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_focused="false" android:state_pressed="false" android:drawable="@drawable/guide_btn_red" />
    <item android:state_focused="false" android:state_pressed="true" android:drawable="@drawable/guide_btn_red_pressed" />
    <item android:state_focused="true" android:state_pressed="false" android:drawable="@drawable/guide_btn_red_pressed" />
    <item android:state_focused="true" android:state_pressed="true" android:drawable="@drawable/guide_btn_red_pressed" />
</selector>

把这个selector作为图片设置为Button的背景即可。

<Button
            android:id="@+id/guide_activity_btn"
            android:layout_width="216dip"
            android:layout_height="45dip"
            android:background="@drawable/guide_go_button"
            android:shadowColor="#7f000000"
            android:shadowDx="0.0"
            android:shadowDy="1.0"
            android:shadowRadius="1.0"
            android:text="开启音乐之窗"
            android:textColor="#ffffffff"
            android:textSize="14sp" />

推荐阅读更多精彩内容