我需要在
android中做背景颜色变化的圆角按钮.
我该怎么办?
示例链接/代码非常感谢.
解决方法
你想使用
Android的Shape Drawables.
http://developer.android.com/guide/topics/resources/drawable-resource.html#Shape
http://developer.android.com/guide/topics/resources/drawable-resource.html#Shape
绘制/ cool_button_background.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<corners
android:radius="@dimen/corner_radius" />
<gradient
android:angle="270"
android:startColor="@color/almost_white"
android:endColor="@color/somewhat_gray"
android:type="linear" />
</shape>
那么你必须从这些形状可绘制中创建一个“选择器”.这使您可以根据状态使按钮显示不同. IE:按压,聚焦等
http://developer.android.com/guide/topics/resources/drawable-resource.html#StateList
绘制/ cool_button.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true"
android:drawable="@drawable/cool_inner_press_bottom" />
<item android:state_focused="true" android:state_enabled="true"
android:state_window_focused="true"
android:drawable="@drawable/cool_inner_focus_bottom" />
<item
android:drawable="@drawable/cool_button_background" />
</selector>
奖金:您可能想要为该按钮创建一个样式,以便您可以在整个程序中保持一致.你可以剪出这个步骤,只需设置按钮的android:background =“@ drawable / cool_button”.
价值观/ styles.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="MyCoolButton">
<item name="android:background">@drawable/cool_button_background</item>
</style>
</resources>
最后,按钮!
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:background="@drawable/appwidget_bg">
<Button
android:id="@+id/btnAction"
android:layout_width="wrap_content"
android:layout_weight="wrap_content"
style="@style/CoolButton"
/>
</LinearLayout>