Android自定义Button按钮
2013-04-03 15:12
288 查看
Android自定义Button按钮主要可以分成两种形式:
1.通过自定MyButton类来继承Button,将所有效果在类中实现.
2.通过xml文件来改变Button的样式和颜色.
今天我就先讲通过xml文件,稍后封装自定义Button类再补上.
TestcActivity
testc.xml
button_style.xml
gradient 主体渐变 startColor开始颜色,endColor结束颜色 ,angle开始渐变的角度(值只能为90的倍数,0时为左到右渐变,90时为下到上渐变,依次逆时针类推)
stroke 边框 width 边框宽度,color 边框颜色
corners 圆角 radius 半径,0为直角
padding text值的相对位置
1.通过自定MyButton类来继承Button,将所有效果在类中实现.
2.通过xml文件来改变Button的样式和颜色.
今天我就先讲通过xml文件,稍后封装自定义Button类再补上.
TestcActivity
package com.example.blueapp; import android.app.Activity; import android.os.Bundle; public class TestcActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.testc); } }
testc.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <Button android:layout_width="fill_parent" android:layout_height="wrap_content" android:textSize="20dp" android:text="测试按钮" android:background="@drawable/button_style" /> <Button android:layout_width="fill_parent" android:layout_height="wrap_content" android:textSize="20dp" android:text="原始按钮" /> </LinearLayout>
button_style.xml
<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:state_pressed="true"> <shape> <gradient android:startColor="#0d76e1" android:endColor="#0d76e1" android:angle="270" /> <stroke android:width="1dip" android:color="#f403c9" /> <corners android:radius="2dp" /> <padding android:left="10dp" android:top="10dp" android:right="10dp" android:bottom="10dp" /> </shape> </item> <item android:state_focused="true"> <shape> <gradient android:startColor="#ffc2b7" android:endColor="#ffc2b7" android:angle="270" /> <stroke android:width="1dip" android:color="#f403c9" /> <corners android:radius="2dp" /> <padding android:left="10dp" android:top="10dp" android:right="10dp" android:bottom="10dp" /> </shape> </item> <item> <shape> <gradient android:startColor="#000000" android:endColor="#ffffff" android:angle="180" /> <stroke android:width="1dip" android:color="#f403c9" /> <corners android:radius="5dip" /> <padding android:left="10dp" android:top="10dp" android:right="10dp" android:bottom="10dp" /> </shape> </item> </selector>
gradient 主体渐变 startColor开始颜色,endColor结束颜色 ,angle开始渐变的角度(值只能为90的倍数,0时为左到右渐变,90时为下到上渐变,依次逆时针类推)
stroke 边框 width 边框宽度,color 边框颜色
corners 圆角 radius 半径,0为直角
padding text值的相对位置
相关文章推荐
- Android ImageButton自定义按钮的按下效果的代码实现方法
- Android - 点击可以转动的自定义右下角浮动FABImageButton按钮
- Android中ImageButton自定义按钮的按下效果的代码实现方法,附网上2种经典解决方法。
- Android中ImageButton自定义按钮的按下效果的代码实现方法,附网上2种经典解决方法
- Android中ImageButton自定义按钮的按下效果的代码实现方法,附网上2种经典解决方法。 .
- Android自定义Button按钮显示样式
- Android中ImageButton自定义按钮的按下效果的代码实现方法,附网上2种经典解决方法。
- Androidの自定义圆角按钮button
- Android中ImageButton自定义按钮的按下效果的代码实现方法,附网上2种经典解决方法
- Android应用开发笔记(10):制作自定义背景Button按钮、自定义形状Button的全攻略
- Android ImageButton自定义按钮的按下效果的代码实现方法分享
- Android使用xml自定义Button按钮显示样式
- Android之自定义背景Button按钮、自定义形状Button的全攻略、设置字体颜色
- Android自定义Button按钮显示样式
- Android自定义Button按钮显示样式
- Android自定义可拖拽的悬浮按钮---DragFloatingActionButton
- Android自定义view 之 圆环ImageView 和 圆角按钮Button
- Android-Flat-Button自定义带阴影效果按钮
- android背景选择器selector用法、自定义Button按钮样式..........
- 转载 :Android中ImageButton自定义按钮的按下效果的代码实现方法,附网上2种经典解决方法。