您的位置:首页 > 移动开发 > Android开发

一只大二狗的Android历程--简单自定义按钮样式(Custom Button Style)

2017-03-06 22:58 417 查看
2017年3月6日 10:20PM

距离上次写博客已经有5天的时间了,维修队里要准备雷锋月的活动着实花了我不少精力,到处跑赞助,送申请表,,╮(╯_╰)╭

今天写个简短的博客,用一个例子来演示在安卓开发中简单自定义Button的样式(话说这样的博文已经烂大街了吧 。。)

更新说明:下一篇内容重点内容 发现不用这么麻烦,而且还能做渐变色的点击颜色。。。

现在要演示的例子中包含了按钮的圆角,渐变色,点击颜色

<!--这个是放在drawable里的button_style.xml-->

<?xml version="1.0" encoding="UTF-8"?>
<shape
xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<!--渐变色-->
<gradient
android:startColor="#8DEEEE"
android:endColor="#FF8247"
android:angle="90" />
<!--圆角角度,值越大越圆-->
<corners android:radius="5dip" />
<!--按钮内的文字位置居中-->
<padding
android:left="10dp"
android:top="10dp"
android:right="10dp"
android:bottom="10dp"
/>
</shape>


<!--这个是放在drawable里的button_selector.xml-->

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

<!--这个是放在values/string.xml里的-->

<resources>
<string name="app_name">Button</string>
<drawable name="unpressed">#ff6501</drawable>
<drawable name="pressed">#a44100</drawable>
</resources>


<!--这个是写在activity_main.xml里的-->

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.eee.button.MainActivity">

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/button_style"
android:text="Button_Style"
android:id="@+id/button"
android:layout_marginTop="117dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />

<Button
android:text="Press_change_color"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/button"
android:layout_centerHorizontal="true"
android:layout_marginTop="62dp"
android:background="@drawable/button_selector"
/>

</RelativeLayout>


效果图





PS:请原谅的我的审美吧,感觉略丑啊 = =
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  android