【android基础知识】【selector】【自定义控件】

项目demo源码:http://download.csdn.net/detail/mcdullsin/8291231

虽然自己还是一个学的比较多的新人,但是真心希望所有博客都附上源码,像我这类人也能缩短学习路程。

效果:

好了,首先来看一下图片选择器selector。

btn.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >

    <item android:state_focused="true" android:state_pressed="false" android:drawable="@drawable/btn_normal"></item>
    <item android:state_pressed="true" android:drawable="@drawable/btn_white"></item>
    <item android:state_checked="true" android:drawable="@drawable/btn_white"></item>
    <item android:state_focused="false" android:state_pressed="false" android:drawable="@drawable/btn_normal"></item>

</selector>

使用selector的组件:

<cn.com.karl.view.ImageBtn
        android:id="@+id/btn_right"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:background="@drawable/btn"
        />

将其当成静态图片就可以了。

这个selector定义了四种状态下的按钮图片背景,即:未获得焦点未点击、获得焦点未点击、点击、选中。不同状态下图片背景也不同。使用selector最好有一个初始图片。我这个是针对按钮说的,有可能listview有不同?没进行过下一步试验。

关于selector和shape非常不错的一篇博文:http://blog.csdn.net/tianjf0514/article/details/7492876

二、自定义控件

1、 首先定义一个layout实现按钮内部布局:

<?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="horizontal" >

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical"
        android:paddingBottom="5dip"
        android:layout_marginLeft="8dp"
        android:paddingTop="5dip" />

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical"
        android:layout_marginLeft="8dip"
        android:text="确定"
        android:layout_marginRight="8dp"
        android:textColor="#000000" />

</LinearLayout>

2、接下来写一个类继承LinearLayout,导入刚刚的布局,并且设置需要的方法(设置文字,设置图片的方法),从而使的能在代码中控制这个自定义控件内容的显示。

public class ImageBtn extends LinearLayout {

   private ImageView imageView;
   private TextView  textView;

   public ImageBtn(Context context) {
       super(context);
       // TODO Auto-generated constructor stub
   }
   public ImageBtn(Context context, AttributeSet attrs) {
       super(context, attrs);
       // TODO Auto-generated constructor stub
       LayoutInflater inflater=(LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
       inflater.inflate(R.layout.imagebtn, this);
       imageView=(ImageView) findViewById(R.id.imageView1);
       textView=(TextView)findViewById(R.id.textView1);
   }

   /**
    * 设置图片资源
    */
   public void setImageResource(int resId) {
       imageView.setImageResource(resId);
   } 

   /**
    * 设置显示的文字
    */
   public void setTextViewText(String text) {
       textView.setText(text);
   } 

}

在上面的代码中重要的是:

LayoutInflater inflater=(LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
inflater.inflate(R.layout.imagebtn, this);
imageView=(ImageView) findViewById(R.id.imageView1);
textView=(TextView)findViewById(R.id.textView1);

之后再主布局文件中引用并在activity中找到它们,添加事件。

主布局文件:

<?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="horizontal" >

    <cn.com.karl.view.ImageBtn
        android:id="@+id/btn_right"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:background="@drawable/btn"
        />

    <cn.com.karl.view.ImageBtn
        android:id="@+id/btn_error"
        android:layout_marginLeft="5dp"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:background="@drawable/btn"
        />

</LinearLayout>

java文件:

public class MainActivity extends Activity {
  private ImageBtn imageBtn1;
  private ImageBtn imageBtn2;
   @Override
   protected void onCreate(Bundle savedInstanceState) {
       // TODO Auto-generated method stub
       super.onCreate(savedInstanceState);
       setContentView(R.layout.activity_main);

       imageBtn1=(ImageBtn) this.findViewById(R.id.btn_right);
       imageBtn2=(ImageBtn) this.findViewById(R.id.btn_error);
       imageBtn1.setTextViewText("确定");
       imageBtn2.setTextViewText("取消");
       imageBtn1.setImageResource(R.drawable.right_icon);
       imageBtn2.setImageResource(R.drawable.error_icon);

       imageBtn1.setOnClickListener(new View.OnClickListener() {

           public void onClick(View v) {
               // TODO Auto-generated method stub
               Toast.makeText(getApplicationContext(), "点击的正确按钮", 1).show();
           }
       });

       imageBtn2.setOnClickListener(new View.OnClickListener() {

           public void onClick(View v) {
               // TODO Auto-generated method stub
               Toast.makeText(getApplicationContext(), "点击的错误按钮", 1).show();
           }
       });
   }
}
时间: 2024-08-06 02:29:34

【android基础知识】【selector】【自定义控件】的相关文章

Android基础知识(6)—数据持久化之数据存储

阅读前,请浏览此处上方目录. Android基础知识(6)-数据持久化之数据存储 本章内容为个人笔记,参考书籍有:<疯狂的android>第3版.<第一行代码> 首先,我们要知道什么是数据持久化. 数据持久化就是指那些内存中的瞬时数据保存到存储设备中,保证即使手机在关机的情况下,这些数据不会丢失.保存在内存中的数据是处于瞬时状态,保存在存储设备中的数据是处于持久状态.持久化技术则是提供了一种机制可以让数据在瞬时状态和持久状态之间进行转换. Android系统主要提供了三种方式用于简

android基础知识13:AndroidManifest.xml文件解析

1.重要性 AndroidManifest.xml是Android应用程序中最重要的文件之一.它是Android程序的全局配置文件,是每个 android程序中必须的文件.它位于我们开发的应用程序的根目录下,描述了package中的全局数据,包括package中暴露的组件 (activities, services, 等等),以及他们各自的实现类,各种能被处理的数据和启动位置等重要信息. 因此,该文件提供了Android系统所需要的关于该应用程序的必要信息,即在该应用程序的任何代码运行之前系统所

Android基础知识【项目实训】【1】

[该项目实训是Android基础知识的一个综合练习] [项目题目]:校园订餐App设计 综合案例 [设计目标] 1.必要功能 ?快餐店浏览,与订餐 ?今天订餐活动查询与订餐,特价饭菜预定 ?分类订餐查询,预定 ?常定饭菜  预定 ?健康餐推荐 ?定时预定,提前预定 ?订单查看, ?餐馆与饭菜打分,评价 ?用户注册与登录 2.扩展选择功能 ?快速拨打电话 ?饮食跟踪,热量估算 ?系统设置 [项目说明] 该项目为实际应用项目的单机 简化版本,只需要完成Android平台App的设计与开发工作. Ap

Android基础知识【项目实训】【2】

[该项目实训是Android基础知识的一个综合练习,特别提示:项目中会用到一些图片素材,都是随意整理的,稍后会上传一个资源,包含该事项项目的基本功能,也含有图片素材] [项目题目]:校园订餐App设计 综合案例 [目标] 因为项目只涉及基础知识,因此项目中所用数据并不联网,都读取单机数据库.(即将该项目中所用数据,如菜品信息.店铺信息等存入数据库)用户在第一次打开该项目时,会在用户手机上创建这些数据库,并插入测试数据. 1.先制作一个欢迎界面,欢迎的同时,准备数据库 欢迎界面Activity对应

Android基础知识【项目实训】【3】

[该项目实训是Android基础知识的一个综合练习,特别提示:项目中会用到一些图片素材,都是随意整理的,稍后会上传一个资源,包含该事项项目的基本功能,也含有图片素材] [项目题目]:校园订餐App设计 综合案例 [目标] 欢迎界面过后,应该显示app的主界面了,根据[UI设计指导]中的规划,主界面采用上下两级标签导航.这部分是app开发中比较麻烦的一块. 1.先来看一下,最终的效果吧,这样做起来比较有底: 默认显示的主界面,下部是主导航,上面是二级导航,默认打开的是"促销打折"这一版面

Android基础知识【项目实训】【4】

[该项目实训是Android基础知识的一个综合练习,特别提示:项目中会用到一些图片素材,都是随意整理的,稍后会上传一个资源,包含该事项项目的基本功能,也含有图片素材] [项目题目]:校园订餐App设计 综合案例 [目标] 主界面的功能确实比较复杂,因此上一篇知识说的周边内容.现在开始说这个界面的代码和布局文件. 1.先看一下项目的组织结构吧,要不然不好说他们的关系: (1)db包中的都是跟 数据库相关的 (2)eatall中放的都是activity或者fragment (3)entity中放的实

Android基础知识【项目实训】【5】

[该项目实训是Android基础知识的一个综合练习,特别提示:项目中会用到一些图片素材,都是随意整理的,稍后会上传一个资源,包含该事项项目的基本功能,也含有图片素材] [项目题目]:校园订餐App设计 综合案例 [目标] 主界面中包含两个二级子界面,分别是活动界面和账单界面,下面介绍它们的实现代码和布局文件. 1.下面这个是 活动界面的Activity代码,因为这个界面加载时需要 读取数据库中数据了,所有功能的实现上会涉及到 db那个包中一些类. 注意这个Activity也是继承 Activit

看看android基础知识,谁帮我作答

不管怎么着,了解一点android的基本知识还是有必要的,就当开阔一些自己的眼界吧.... android的四大功能组件是_activity_,_service_,_BroadcastReceive广播接收器_,_Content Provider_. android的系统架构是android,_java_虚拟机和_linux_操作系统. 在Activity的___distoryed______状态和__stop_情况下,系统可能会回收Activity. ActivityA中的某个Button的o

(Android 基础知识review)打电话

1.main.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基础知识

1. 前言 1.1. 什么是3G.4G Ÿ 第三代移动通信技术(3rd - Generation),速率一般在几百Kbps,较之前的2G和2.5G在数据传输速度上有很大提升. Ÿ 第四代移动通信技术(4th - Generation),速度可达到100Mbps以上,几乎可以满足人们的所有传输数据的需求. Ÿ 目前主流的3G技术标准有三种: WCDMA:全球80%以上的3G网络都是采用此种制式.中国联通运营.186 CDMA2000:目前日韩及北美使用较多.中国电信运营. 189 TD-SCDMA