简洁明了的回答:
在 res/values/colors.xml 中创建颜色资源。
<?xml version="1.0" encoding="utf-8"?>
<resources>
<item name="yellow" type="color">#F7B500</item>
<item name="yellow_dark" type="color">#AC7E00</item>
<integer-array name="androidcolors">
<item>@color/yellow</item>
<item>@color/yellow_dark</item>
</integer-array>
</resources>
创建一个可绘制对象,路径为res/drawable/bg_yellow_round.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<solid android:color="@color/yellow" />
<stroke
android:width="2dp"
android:color="@color/yellow" />
<corners
android:bottomLeftRadius="20dp"
android:bottomRightRadius="20dp"
android:topLeftRadius="20dp"
android:topRightRadius="20dp" />
</shape>
创建另一个drawable,您希望在同一位置进行转换并将其命名为
res/drawable/bg_yellow_dark_round.xml<solid android:color="@color/yellow_dark" />
<stroke
android:width="2dp"
android:color="@color/yellow_dark" />
<corners
android:bottomLeftRadius="20dp"
android:bottomRightRadius="20dp"
android:topLeftRadius="20dp"
android:topRightRadius="20dp" />
现在在 res/color/btn_selector_yellow.xml 中创建一个颜色状态列表。
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" android:exitFadeDuration="@android:integer/config_shortAnimTime">
<item android:drawable="@color/yellow" android:state_focused="true" android:state_pressed="false"/>
<item android:drawable="@drawable/bg_yellow_dark_round" android:state_pressed="true"/>
<item android:drawable="@drawable/bg_yellow_round"/>
</selector>
现在按照以下方式将其设置为您的按钮
<Button
android:id="@+id/button1"
android:layout_width="248dp"
android:layout_height="44dp"
android:layout_gravity="center_horizontal"
android:layout_marginBottom="10dp"
android:layout_marginTop="20dp"
android:background="@color/btn_selector_yellow"
android:text="AZ_ is so cool" />
现在这将会从
进行过渡到
.
-
<corners android:radius="15dip"></corners> <solid android:color="#febd26"/> </shape></item> <item> <shape android:shape="rectangle" > <gradient android:startColor="#DD000000" android:endColor="#DD2d2d2d" android:angle="90"></gradient> <corners android:radius="15dip"></corners> </shape> </item> </selector> - Khawar Raza
09-13 15:25:02.868: ERROR/AndroidRuntime(9129): FATAL EXCEPTION: main android.view.InflateException: Binary XML file line #13: Error inflating class <unknown>.
我正在将选择器设置为RoundedImageView
的android:background
属性 [ https://github.com/vinc3m1/RoundedImageView#usage]。有人有任何想法如何解决这个问题吗? - Etienne Lawlor