当前位置:操作系统 > 安卓/Android >>

Android自定义Button按钮

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

1.通过自定MyButton类来继承Button,将所有效果在类中实现.

2.通过xml文件来改变Button的样式和颜色.

今天我就先讲通过xml文件,稍后封装自定义Button类再补上.

TestcActivity

[html]
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); 
    } 

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

[html]
<?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> 

<?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

[html]
<?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&quo

补充:移动开发 , Android ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,