android的selector,shape,gradient


  引用:http://marshal.easymorse.com/archives/3059

写了个简单的android编写自定义效果的按钮,现在还不完整。不过效果出来了。见:

 

 

实现按钮,这里没有通过Button类或者子类去做派生,而是通过TextView派生出来的。在这里三个按钮是三个TextView派生类实例,中间的白线,是1px宽的白色矩形,这样就可以做出类似上面的效果。

看布局文件:main.xml

    <?xml version="1.0" encoding="utf-8"?>     
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"     
        android:orientation="vertical" android:layout_width="fill_parent"     
        android:layout_height="fill_parent" android:background="@drawable/background_color">     
        <LinearLayout android:layout_width="fill_parent"     
            android:layout_height="10dip" />     
        <LinearLayout android:layout_width="fill_parent"     
            android:layout_height="40dip">     
            <com.easymorse.textbutton.TextButton     
                android:layout_width="fill_parent" android:layout_height="fill_parent"     
                android:layout_weight="1" android:text="电影"     
                android:gravity="center_vertical|center_horizontal"     
                android:background="@drawable/button" android:focusable="true" android:clickable="true"/>     
            <View android:layout_width="2px" android:layout_height="fill_parent"     
                android:background="#ffffffff" />     
            <com.easymorse.textbutton.TextButton     
                android:layout_width="fill_parent" android:layout_height="fill_parent" android:clickable="true"     
                android:layout_weight="1" android:text="电视"     
                android:gravity="center_vertical|center_horizontal"     
                android:background="@drawable/button" android:focusable="true" />     
            <View android:layout_width="2px" android:layout_height="fill_parent"     
                android:background="#ffffffff" />     
            <com.easymorse.textbutton.TextButton     
                android:layout_width="fill_parent" android:layout_height="fill_parent" android:clickable="true"     
                android:layout_weight="1" android:text="明星"     
                android:gravity="center_vertical|center_horizontal"     
                android:background="@drawable/button" android:focusable="true" />     
        </LinearLayout>     
    </LinearLayout>    

 

 drawable文件夹下有background_color.xml和button.xml

background_color.xml

 

    <?xml version="1.0" encoding="utf-8" ?>     
    <shape xmlns:android="http://schemas.android.com/apk/res/android">    
        <!-- <solid android:color="#f0600000" /> -->     
        <!-- <stroke android:width="3dp" color="#ffff8080" /> -->     
        <!-- <corners android:radius="3dp" /> -->     
        <!-- <padding android:left="10dp" android:top="10dp" android:right="10dp" -->     
        <!-- android:bottom="10dp" />   
        -->     
        <gradient android:startColor="#FFFFFFFF"     
            android:endColor="#FFFFFFFF"     
            android:angle="270.0"     
            android:centerY="0.3"     
            android:centerColor="#FFBDBDBD" />     
    </shape>    

 

这里需要注意的几点:

  • 对于布局的像素设置,一般要用dip,这样在更大或者更小的屏幕下展示可以自动适配,如果是px,是物理像素,这样在小的屏幕里可能会显得大,在大的屏幕中显小
  • 在按钮布局中要使用android:focusable="true" android:clickable="true",这样才能比如通过轨迹球聚焦到按钮上,才能用手触摸按钮的时候触发事件
  • 点击按钮变色,主要在android:background="@drawable/button"配置,button配置了点击事件发生后的背景色改变,而不需要编写代码

下面来看看drawable/button.xml文件:

 

    <?xml version="1.0" encoding="utf-8"?>     
    <selector xmlns:android="http://schemas.android.com/apk/res/android"     
        android:constantSize="true">     
        <item android:state_focused="true">     
            <shape>     
                <gradient android:startColor="#FFE5CF33" android:endColor="#FFF1E7A2"     
                    android:angle="90.0">     
                </gradient>     
            </shape>     
        </item>     
        <item android:state_enabled="true" android:state_pressed="false">     
            <shape>     
                <gradient android:startColor="#FF1B1B1B" android:endColor="#FF969696"     
                    android:angle="90.0">     
                </gradient>     
            </shape>     
        </item>     
        <item android:state_enabled="true" android:state_pressed="true">     
            <shape>     
                <gradient android:startColor="#FF000000" android:endColor="#FF474747"     
                    android:angle="90.0">     
                </gradient>     
            </shape>     
        </item>     
        <item android:state_enabled="false" android:state_pressed="true">     
            <shape>     
                <gradient android:startColor="#FF000000" android:endColor="#FF474747"     
                    android:angle="90.0">     
                </gradient>     
            </shape>     
        </item>     
        <item>     
            <shape>     
                <gradient android:startColor="#FF000000" android:endColor="#FF474747"     
                    android:angle="90.0">     
                </gradient>     
            </shape>     
        </item>     
    </selector>   

 

这个文件中定义了当条目(也就是按钮)enable和(或)pressed的情况下的背景渐近色的配置情况。

实际上,上面介绍的部分,说一下TextView的派生类

    package com.easymorse.textbutton;     
        
    import android.content.Context;     
    import android.util.AttributeSet;     
    import android.view.MotionEvent;     
    import android.view.View;     
    import android.widget.TextView;     
    import android.widget.Toast;     
        
    public class TextButton extends TextView {     
        
        public TextButton(Context context) {     
            super(context);     
        }     
        
        public TextButton(Context context, AttributeSet attrs, int defStyle) {     
            super(context, attrs, defStyle);     
        }     
        
     }    
    package com.easymorse.textbutton;    
        
    import android.app.Activity;    
    import android.os.Bundle;    
        
    public class PuzzleActivity extends Activity{    
         /** Called when the activity is first created. */    
        @Override    
        public void onCreate(Bundle savedInstanceState) {    
            super.onCreate(savedInstanceState);    
            setContentView(R.layout.main);    
        }    
    }    

于listview和button都要改变android原来控件的背景,

android的selector的用法。
首先android的selector是在drawable/xxx.xml中配置的。
先看一下listview中的状态:
把下面的XML文件保存成你自己命名的.xml文件(比如list_item_bg.xml),在系统使用时根据ListView中的列表项的状态来使用相应的背景图片。
drawable/list_item_bg.xml

<?xml version="1.0" encoding="utf-8" ?>   
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- 默认时的背景图片 -->  
  <item android:drawable="@drawable/pic1" />   
<!-- 没有焦点时的背景图片 -->  
  <item android:state_window_focused="false"   
        android:drawable="@drawable/pic1" />   
<!-- 非触摸模式下获得焦点并单击时的背景图片 -->  
  <item android:state_focused="true" android:state_pressed="true"   
        android:drawable= "@drawable/pic2" />  
<!-- 触摸模式下单击时的背景图片 -->  
  <item android:state_focused="false" android:state_pressed="true"   
        android:drawable="@drawable/pic3" />   
<!--选中时的图片背景  -->  
  <item android:state_selected="true"   
        android:drawable="@drawable/pic4" />   
<!--获得焦点时的图片背景  -->  
  <item android:state_focused="true"   
        android:drawable="@drawable/pic5" />   
</selector>

 

使用些xml文件:第一种是在listview中配置android:listSelector="@drawable/list_item_bg 或者在listview的item中添加属性android:background=“@drawable/list_item_bg"即可实现,或者在java代码中使用:Drawable drawable = getResources().getDrawable(R.drawable.list_item_bg); ListView.setSelector(drawable);同样的效果。 但是这样会出现列表有时候为黑的情况,需要加上:android:cacheColorHint="@android:color/transparent"使其透明。 其次再来看看Button的一些背景效果: android:state_selected是选中 android:state_focused是获得焦点 android:state_pressed是点击 android:state_enabled是设置是否响应事件,指所有事件 根据这些状态同样可以设置button的selector效果。也可以设置selector改变button中的文字状态。 以下就是配置button中的文字效果:

drawable/button_font.xml

 

 

<?xml version="1.0" encoding="utf-8"?> 
<selector xmlns:android="http://schemas.android.com/apk/res/android">
 <item android:state_selected="true" android:color="#FFF" />
 <item android:state_focused="true" android:color="#FFF" /> 
<item android:state_pressed="true" android:color="#FFF" /> <item android:color="#000" />
 </selector>

 

 


Button还可以实现更复杂的效果-渐变

  1. drawable/button_color.xml    

 

<?xml version="1.0" encoding="utf-8"?>    
<selector xmlns:android="http://schemas.android.com/apk/res/android"> /    
<item android:state_pressed="true">//定义当button 处于pressed 状态时的形态。    
<shape>    
<gradient android:startColor="#8600ff" />    
<stroke android:width="2dp" android:color="#000000" />    
<corners android:radius="5dp" />    
<padding android:left="10dp" android:top="10dp"    
android:bottom="10dp" android:right="10dp"/>    
</shape>    
</item>    
<item android:state_focused="true">//定义当button获得 focus时的形态    
<shape>    
<gradient android:startColor="#eac100"/>    
<stroke android:width="2dp" android:color="#333333" color="#ffffff"/>    
<corners android:radius="8dp" />    
<padding android:left="10dp" android:top="10dp"    
android:bottom="10dp" android:right="10dp"/>    
</shape>    
</item>    
</selector>     

 

最后,需要在包含 button的xml文件里添加两项。假如是 main.xml 文件,我们需要在<Button />里加两项。
       android:focusable="true"
       android:backgroud="@drawable/button_color"
       这样当你使用Button的时候就可以甩掉系统自带的那黄颜色的背景了,实现个性化的背景,配合应用的整体布局非常之有用啊。

 

drawable/button.xml里的selector,shape,gradient。

shape是用来定义形状的,gradient定义该形状里面为渐变色填充,startColor起始颜色,endColor结束颜色,angle表示方向角度。当angle=0时,渐变色是从左向右。 然后逆时针方向转,当angle=90时为从下往上

posted @ 2012-08-19 00:40  elon  阅读(368)  评论(0)    收藏  举报