我平時做這樣的事!
你可以在一個XML中完成所有這些工作,但是我將展示你更好地理解的很長一段路。
simple_button.xml
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:state_pressed="true"
android:drawable="@drawable/button_pressed"/>
<item
android:state_enabled="false"
android:drawable="@drawable/button_disabled"/>
<item
android:state_enabled="true"
android:drawable="@drawable/button_enabled"/>
</selector>
button_pressed.xml
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<corners android:radius="2dp" />
<solid android:color="#20FF5252" />
<padding
android:bottom="0dp"
android:left="0dp"
android:right="0dp"
android:top="0dp" />
<size
android:width="100dp"
android:height="35dp" />
<stroke
android:width="1dp"
android:color="#1DE9B6" />
</shape>
button_disabled.xml
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle" >
<corners
android:radius="14dp"
/>
<solid
android:color="@android:color/transparent"
/>
<padding
android:left="0dp"
android:top="0dp"
android:right="0dp"
android:bottom="0dp"
/>
<stroke
android:width="8dp"
android:color="#1DE9B6"
/>
</shape>
button_enabled.xml
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle" >
<corners
android:radius="2dp"
/>
<solid
android:color="@android:color/transparent"
/>
<padding
android:left="0dp"
android:top="0dp"
android:right="0dp"
android:bottom="0dp"
/>
<size
android:width="260dp"
android:height="50dp"
/>
<stroke
android:width="1dp"
android:color="#1DE9B6"
/>
</shape>
styles.xml
<style name="Widget.Button.Simple" parent="android:Widget">
<item name="android:gravity">center_vertical|center_horizontal</item>
<item name="android:background">@drawable/simple_button</item>
<item name="android:textAppearance">?android:textAppearanceMedium</item>
<item name="android:textColor">#1DE9B6</item>
<item name="android:textStyle">bold</item>
</style>
使用
<Button
android:id="@+id/btn_simple"
style="@style/Widget.Button.Simple"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_margin="20dp"
android:text="Button" />
我希望它能幫助!
顯示'@ drawable/button_border'內容 –