Android自定义Button按钮

Android自定义Button按钮主要可以分成两种形式:

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值的相对位置

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值