zoukankan      html  css  js  c++  java
  • Android应用开发学习之Selector

    作者:刘昊昱 

    博客:http://blog.csdn.net/liuhaoyutz

     

    本文我们来看一个通过selector动态改变ImageButton背景图片的例子,下图是该程序的运行效果:

    该程序中有三个ImageButton,最上面的是鼠标按下时的状态;中间的是鼠标没有按下,但是按钮获得了焦点的状态;最下面的是鼠标没有按下,按钮也不具有焦点的状态。

    先来看主布局文件main.xml,其内容如下:

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical" >
        
        <ImageButton 
    		android:id="@+id/button1" 
    		android:src="@drawable/button_image_selector" 
    		android:focusableInTouchMode="true"
    		android:background="#0000"
    		android:layout_width="wrap_content" 
    		android:layout_height="wrap_content">
    	</ImageButton>
    	
        <ImageButton 
    		android:id="@+id/button2" 
    		android:src="@drawable/button_image_selector" 
    		android:focusableInTouchMode="true"
    		android:background="#0000"
    		android:layout_width="wrap_content" 
    		android:layout_height="wrap_content">
    	</ImageButton>
    	
        <ImageButton 
    		android:id="@+id/button3" 
    		android:src="@drawable/button_image_selector" 
    		android:focusableInTouchMode="true"
    		android:background="#0000"
    		android:layout_width="wrap_content" 
    		android:layout_height="wrap_content">
    	</ImageButton>
    
    </LinearLayout>
    


    在线性布局控件中,放置三个ImageButton,android:src属性指定了背景图片选择器button_image_selector.xml,android:focusableInTouchMode属性标明按钮可以获得焦点。android:background="#0000"属性是为了去掉按钮背后的灰色边框,但是这样做,如果不用背景选择器的话,会看不出点击效果。

    按钮背景图片选择器button_image_selector.xml定义如下:

    <?xml version="1.0" encoding="utf-8"?>
    <selector
      xmlns:android="http://schemas.android.com/apk/res/android">
        <item android:state_pressed="true" android:drawable="@drawable/image01"/>
        <item android:state_pressed="false" android:state_focused="true" android:drawable="@drawable/image02"/>
        <item android:state_pressed="false" android:state_focused="false" android:drawable="@drawable/image03"/>
    </selector>
    


    item1指定了鼠标按下时的背景图片,item2指定了鼠标没有按下,但是获得焦点时的背景图片,item3指定了鼠标没有按下,且没有焦点时的背景图片。

    主Activity文件没有什么特别的,其内容如下:

    package com.liuhaoyu;
    
    import java.util.ArrayList;
    import java.util.HashMap;
    import java.util.List;
    import java.util.Map;
    
    import android.app.Activity;
    import android.os.Bundle;
    import android.widget.ArrayAdapter;
    import android.widget.ListView;
    import android.widget.SimpleAdapter;
    
    public class MainActivity extends Activity {
        /** Called when the activity is first created. */
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
        }
    }
    
  • 相关阅读:
    字符串匹配算法 【微软面试100题 第三十三题】
    交换元素,使两数组之和的差最小 【微软面试100题 第三十二题】
    在从1到n的正数中1出现的次数 【微软面试100题 第三十题】
    栈的push、pop序列 【微软面试100题 第二十九题】
    整数的二进制表示中1的个数 【微软面试100题 第二十八题】
    跳台阶问题 【微软面试100题 第二十七题】
    左旋转字符串 【微软面试100题 第二十六题】
    字符串中找出最长的数字串 【微软面试100题 第二十五题】
    合并链表 【微软面试100题 第二十四题】
    计算圆形是否和正方形相交 【微软面试100题 第二十三题】
  • 原文地址:https://www.cnblogs.com/riskyer/p/3231110.html
Copyright © 2011-2022 走看看