你好,游客 登录 注册 搜索
背景:
阅读新闻

Android实战技巧之组合控件

[日期:2012-04-15] 来源:Linux社区  作者:lincyang [字体:大 中 小]
加入自己包名的索引
  1. xmlns:linc="http://schemas.Android.com/apk/res/com.linc.game"  
剩下的就一目了然了。

LevelMenu.java

  1. package com.linc.game;  
  2.   
  3. import com.linc.game.LevelMenuItem.OnItemClickListener;  
  4. import android.content.Context;  
  5. import android.util.AttributeSet;  
  6. import android.util.Log;  
  7. import android.view.LayoutInflater;  
  8. import android.widget.LinearLayout;  
  9.   
  10. public class LevelMenu extends LinearLayout {  
  11.     private LevelMenuItem item1,item2,item3;  
  12.       
  13.     public LevelMenu(Context context) {  
  14.         super(context);  
  15.           
  16.     }  
  17.   
  18.       
  19.     public LevelMenu(Context context, AttributeSet attrs) {  
  20.         super(context, attrs);  
  21.         LayoutInflater layoutInflater = (LayoutInflater) context.  
  22.             getSystemService(Context.LAYOUT_INFLATER_SERVICE);  
  23.         layoutInflater.inflate(R.layout.level_menu, this);  
  24.         initWidget();  
  25.     }  
  26.     private void initWidget()  
  27.     {  
  28.         item1 = (LevelMenuItem)findViewById(R.id.item1);  
  29.         item2 = (LevelMenuItem)findViewById(R.id.item2);  
  30.         item3 = (LevelMenuItem)findViewById(R.id.item3);  
  31.           
  32.         item1.setOnClickListener(new OnItemClickListener() {  
  33.             @Override  
  34.             public void onImageClick() {  
  35.                 Log.e("dfjdkfjd","dfdfd");  
  36.             }  
  37.         });  
  38.     }  
  39. }  
在处理图片点击事件的时候,我用到了选择器(selector),这是我们实际开发中最常用的小技巧了。它能描述的状态很多,各位看官可以去查查。
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <selector xmlns:android="http://schemas.android.com/apk/res/android" >  
  3.     <item android:state_pressed="true"  
  4.         android:drawable="@drawable/button_push"/>  
  5.     <item android:drawable="@drawable/orange_button"/>  
  6. </selector>  
好,组合控件的例子先到这里,实际功能在下一个实战技巧中演练。

大家在做自定义控件时需要注意的是:

1、自定义控件类不能是是抽象类

2、要用

  1. (Context context, AttributeSet attrs)  
这个构造函数


否则报错:android.view.InflateException: Binary XML file line #15: Error inflating cla。。。

linux
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款