然后我们就要写一个类,我这继承子线性布局。有两个构造函数,我们主要在两个参数的函数中工作。
- public class LevelMenuItem extends LinearLayout {
- public LevelMenuItem(Context context, AttributeSet attrs) {
- super(context, attrs);
- }
此类完整代码:
- package com.linc.game;
- import Android.content.Context;
- import android.content.res.TypedArray;
- import android.util.AttributeSet;
- import android.view.LayoutInflater;
- import android.view.View;
- import android.widget.ImageView;
- import android.widget.LinearLayout;
- import android.widget.TextView;
- /**
- * 自定义一个关卡
- * 共有7个属性,看attr文件
- * 在程序中提供修改这7个属性的接口,
- * 一个自定义控件的任务就算完成。
- * 一个自定义控件的好处就是把一些需要模块化的
- * UI和逻辑放在一起,做到了高内聚,向其他模块提供接口并很少
- * 依赖外界,这样就是低耦合。一个自定义控件就是一个封闭的王国,
- * 这里由你掌控。
- *
- * 编写时,如果遇到在attr里写好属性,但是在这里认不出来,
- * 就clean一下项目。切记。
- *
- * @author linc
- *
- */
- public class LevelMenuItem extends LinearLayout {
- private TextView mTextView = null;
- private ImageView mImageView = null;
- public LevelMenuItem(Context context) {
- super(context);
- }
- public LevelMenuItem(Context context, AttributeSet attrs) {
- super(context, attrs);
- LayoutInflater layoutInflater = (LayoutInflater) context.
- getSystemService(Context.LAYOUT_INFLATER_SERVICE);
- layoutInflater.inflate(R.layout.level_menu_item, this);
- TypedArray typedArray = context.obtainStyledAttributes(attrs
- ,R.styleable.LevelMenuItem);
- initWidget(typedArray);
- }
- private void initWidget(TypedArray typedArray)
- {
- mTextView = (TextView)findViewById(R.id.tv_item);
- String textString = typedArray.getString(R.styleable.LevelMenuItem_text);
- int textColor = typedArray.getColor(R.styleable.LevelMenuItem_text_color,
- 0xffffffff);
- float textSize = typedArray.getDimension(R.styleable.LevelMenuItem_text_size,
- 20);
- mTextView.setText(textString);
- mTextView.setTextColor(textColor);
- mTextView.setTextSize(textSize);
- mImageView = (ImageView)findViewById(R.id.image_item);
- int imageHeight = (int) typedArray.getDimension(R.styleable.LevelMenuItem_image_height, 25);
- int imageWidth = (int) typedArray.getDimension(R.styleable.LevelMenuItem_image_width, 25);
- int imageSrc = typedArray.getResourceId(R.styleable.LevelMenuItem_image_src, 0);
- int imageBg = typedArray.getResourceId(R.styleable.LevelMenuItem_image_bg, 0);
- int imageAlpha = typedArray.getInt(R.styleable.LevelMenuItem_image_alpha, 255);
- mImageView.setAlpha(imageAlpha);
- mImageView.setImageResource(imageSrc);
- mImageView.setBackgroundResource(imageBg);
- LayoutParams layoutParams = new LayoutParams(imageWidth, imageHeight);
- mImageView.setLayoutParams(layoutParams);
- typedArray.recycle();
- }
- /**
- * 设置此控件的文本
- * @param text
- */
- public void setText(String text)
- {
- mTextView.setText(text);
- }
- /**
- * 设置文字颜色
- * @param textColor
- */
- public void setTextColor(int textColor)
- {
- mTextView.setTextColor(textColor);
- }
- /**
- * 设置字体大小
- * @param textSize
- */
- public void setTextSize(int textSize)
- {
- mTextView.setTextSize(textSize);
- }
- /**
- * 设置图片
- * @param resId
- */
- public void setImageResource(int resId)
- {
- mImageView.setImageResource(resId);
- }
- /**
- * 设置图片背景
- */
- public void setBackgroundResource(int resId)
- {
- mImageView.setBackgroundResource(resId);
- }
- /**
- * 设置图片的不透名度
- * @param alpha
- */
- public void setImageAlpha(int alpha)
- {
- mImageView.setAlpha(alpha);
- }
- /**
- * 设置图片的大小
- * 这里面需要使用LayoutParams这个布局参数来设置
- * @param width
- * @param height
- */
- public void setImageSize(int width,int height)
- {
- LayoutParams layoutParams = new LayoutParams(width, height);
- mImageView.setLayoutParams(layoutParams);
- }
- /**
- * image点击事件的回调
- * @param listener
- */
- public void setOnClickListener(OnItemClickListener listener)
- {
- mImageView.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- listener.onImageClick();
- }
- });
- }
- /**
- * 点击事件接口
- * @author linc
- *
- */
- public interface OnItemClickListener
- {
- public void onImageClick();
- }
- }
我要在LevelMenu中用它。xml文件如下:
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:linc="http://schemas.android.com/apk/res/com.linc.game"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:orientation="horizontal">
- <com.linc.game.LevelMenuItem
- android:id="@+id/item1"
- android:layout_width="70dp"
- android:layout_height="80dp"
- linc:text="@string/item1"
- linc:text_size="14sp"
- linc:text_color="#80fa8072"
- linc:image_src="@drawable/orange_button_selector"
- linc:image_alpha="128"
- linc:image_height="48dp"
- linc:image_width="48dp"
- />
- <com.linc.game.LevelMenuItem
- android:id="@+id/item2"
- android:layout_marginLeft="20dp"
- android:layout_width="70dp"
- android:layout_height="80dp"
- linc:text="@string/item2"
- linc:text_size="14sp"
- linc:text_color="#ffeee8aa"
- linc:image_src="@drawable/red_button_selector"
- linc:image_alpha="255"
- linc:image_height="48dp"
- linc:image_width="48dp"
- />
- <com.linc.game.LevelMenuItem
- android:id="@+id/item3"
- android:layout_marginLeft="20dp"
- android:layout_width="70dp"
- android:layout_height="80dp"
- linc:text="@string/item3"
- linc:text_size="14sp"
- linc:text_color="#80cd853f"
- linc:image_src="@drawable/yellow_button_selector"
- linc:image_alpha="128"
- linc:image_height="48dp"
- linc:image_width="48dp"
- />
- </LinearLayout>
