- xmlns:linc="http://schemas.Android.com/apk/res/com.linc.game"
LevelMenu.java
- package com.linc.game;
- import com.linc.game.LevelMenuItem.OnItemClickListener;
- import android.content.Context;
- import android.util.AttributeSet;
- import android.util.Log;
- import android.view.LayoutInflater;
- import android.widget.LinearLayout;
- public class LevelMenu extends LinearLayout {
- private LevelMenuItem item1,item2,item3;
- public LevelMenu(Context context) {
- super(context);
- }
- public LevelMenu(Context context, AttributeSet attrs) {
- super(context, attrs);
- LayoutInflater layoutInflater = (LayoutInflater) context.
- getSystemService(Context.LAYOUT_INFLATER_SERVICE);
- layoutInflater.inflate(R.layout.level_menu, this);
- initWidget();
- }
- private void initWidget()
- {
- item1 = (LevelMenuItem)findViewById(R.id.item1);
- item2 = (LevelMenuItem)findViewById(R.id.item2);
- item3 = (LevelMenuItem)findViewById(R.id.item3);
- item1.setOnClickListener(new OnItemClickListener() {
- @Override
- public void onImageClick() {
- Log.e("dfjdkfjd","dfdfd");
- }
- });
- }
- }
- <?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/button_push"/>
- <item android:drawable="@drawable/orange_button"/>
- </selector>
大家在做自定义控件时需要注意的是:
1、自定义控件类不能是是抽象类
2、要用
- (Context context, AttributeSet attrs)
否则报错:android.view.InflateException: Binary XML file line #15: Error inflating cla。。。
