自定义 android Button 背景,文字,实现动态按钮和按钮颜色渐变

   android Button 切换背景,实现动态按钮和按钮颜色渐变

按钮标签

<Button
            android:id="@+id/button_change"
            android:layout_width="wrap_content" 
            android:layout_height="fill_parent"
            android:textSize="12.0sp" 
            android:layout_marginRight="2.0dip"
            android:text="切换地址"
            android:background="@drawable/title_bar_btn1_bg"
            android:textColor="@drawable/title_bar_btn_text"
            />

按钮背景

<!--设置按钮背景样式 -->
<selector xmlns:android="http://schemas.android.com/apk/res/android"> 
<item android:state_pressed="true">
 <shape>  
     <solid android:color="#6892aa"></solid>
 <!-- <gradient android:startColor="#00a2ff" android:endColor="#0171b1"/>  
 <stroke android:width="1dp" android:color="#00456d" />   
 <corners android:radius="5dp" />   
 <padding android:left="1dp" android:top="1dp"   
 android:bottom="1dp" android:right="1dp"/>    -->
 </shape>  
</item>   
<item android:state_focused="true">
 <shape>   
 <solid android:color="#6892aa"></solid>
 <!-- <gradient android:startColor="#00a2ff" android:endColor="#0171b1"/>  
 <stroke android:width="1dp" android:color="#00456d" />   
 <corners android:radius="5dp" />   
 <padding android:left="1dp" android:top="1dp"   
 android:bottom="1dp" android:right="1dp"/>    -->
 </shape>   
</item>  
<item android:state_hovered="true">
 <shape>  
 <solid android:color="#6892aa"></solid>
 <!-- <gradient android:startColor="#00a2ff" android:endColor="#0171b1"/>  
 <stroke android:width="1dp" android:color="#00456d" />   
 <corners android:radius="5dp" />   
 <padding android:left="1dp" android:top="1dp"   
 android:bottom="1dp" android:right="1dp"/>    -->
 </shape>  
</item>  
</selector>   

按钮文字

<selector xmlns:android="http://schemas.android.com/apk/res/android"> 
<item android:state_pressed="true" android:color="@color/black"/>
<item android:color="@color/white"/>
</selector>   

 

 
    背景选择器相关属性
        android:state_selected :选中
        android:state_focused  :获得焦点
        android:state_pressed  :点击
        android:state_enabled  :设置是否响应事件,指所有事件
二、在java代码中实现背景切换。
1、针对单个按键实现背景切换
  1. public boolean onTouch(View v, MotionEvent event) {    
  2.    Button upStepBtn = (Button) v;    
  3.       if(event.getAction() == MotionEvent.ACTION_DOWN){    
  4.            upStepBtn. getBackground().setAlpha(255);  //设置透明背景  
  5.        }else if(event.getAction() == MotionEvent.ACTION_UP){    
  6.           upStepBtn.setBackgroundResource(R.drawable. R.drawable.bt_from1 );    
  7.           finish();     
  8.        }    
  9.        return false;    
  10.    }    
通过监听按钮的不同状态来更改按钮的背景图片
public boolean onTouch(View v,MotionEvent event){
 
}
参数v:事件源对象
参数event:事件封装类的对象,其中封装了触发事件的详细信息,同样包括事件的类型、触发时间等信息。
event.getAction() == MotionEvent.ACTION_DOWN   ======>按钮被按下
event.getAction() == MotionEvent.ACTION_UP                ======>按钮被释放
2、针对多个按键实现背景切换
  1. private ImageView IB_1,IB_2;  
  2. public boolean onTouch(View v, MotionEvent event) {  
  3. if (v == IB_1) {  
  4. if (event.getAction() == MotionEvent. ACTION_UP ) {  
  5. IB_1.setBackgroundResource(R.drawable.bt_from1);          
  6. } else {  
  7.     IB_1.getBackground().setAlpha(255);//设置透明背景   
  8. }  
  9. if (v == IB_2) {  
  10. if (event.getAction() == MotionEvent. ACTION_UP ) {  
  11. IB_2.setBackgroundResource(R.drawable.bt_from2);  
  12. } else {  
  13.     IB_2.getBackground().setAlpha(255);//  
  14. }             
  15. }  
3、设置BUTTON背景为透明
 在“一”中使用了在筛选器中设置背景颜色设为透明,在“二”中实现在java中设置背景为透明。接下了,详细说一下透明背景的设置。

1、Button或者ImageButton的背景设为透明或者半透明
①、半透明<Button android:background="#e0000000" ... /> 
②、透明<Button android:background="#00000000" ... />
 
 理解:颜色和不透明度 (alpha) 值以十六进制表示法表示。任何一种颜色的值范围都是 0 到 255(00 到 ff)。对于 alpha,00 表示完全透明,ff 表示完全不透明。表达式顺序是“aabbggrr”,其中“aa=alpha”(00 到 ff);“bb=blue”(00 到 ff);“gg=green”(00 到 ff);“rr=red”(00 到 ff)。例如,如果您希望对某叠加层应用不透明度为 50% 的蓝色,则应指定以下值:7fff0000
 
RGB 
设置背景图片透明度:
View v = findViewById(R.id.content);//找到你要设透明背景的layout 的id
v.getBackground().setAlpha(100);//0~255透明度值
设置背景颜色透明度:
ImageView.setBackgroundColor(Color.TRANSPARENT);

posted on 2013-09-30 11:19  woshilee  阅读(1261)  评论(0)    收藏  举报

导航