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

Android实战技巧之组合控件

[日期:2012-04-15] 来源:Linux社区  作者:lincyang [字体:大 中 小]

然后我们就要写一个类,我这继承子线性布局。有两个构造函数,我们主要在两个参数的函数中工作。

  1. public class LevelMenuItem extends LinearLayout {  
  2.       
  3.     public LevelMenuItem(Context context, AttributeSet attrs) {  
  4.         super(context, attrs);  
  5.           
  6.     }  
这个类中我们要完成的工作是,初始化控件属性、提供外部修改属性的接口、控件点击的回调接口。

此类完整代码:

  1. package com.linc.game;  
  2. import Android.content.Context;  
  3. import android.content.res.TypedArray;  
  4. import android.util.AttributeSet;  
  5. import android.view.LayoutInflater;  
  6. import android.view.View;  
  7. import android.widget.ImageView;  
  8. import android.widget.LinearLayout;  
  9. import android.widget.TextView;  
  10. /** 
  11.  * 自定义一个关卡 
  12.  * 共有7个属性,看attr文件 
  13.  * 在程序中提供修改这7个属性的接口, 
  14.  * 一个自定义控件的任务就算完成。 
  15.  * 一个自定义控件的好处就是把一些需要模块化的 
  16.  * UI和逻辑放在一起,做到了高内聚,向其他模块提供接口并很少 
  17.  * 依赖外界,这样就是低耦合。一个自定义控件就是一个封闭的王国, 
  18.  * 这里由你掌控。 
  19.  *  
  20.  * 编写时,如果遇到在attr里写好属性,但是在这里认不出来, 
  21.  * 就clean一下项目。切记。 
  22.  *  
  23.  * @author linc 
  24.  * 
  25.  */  
  26. public class LevelMenuItem extends LinearLayout {  
  27.     private TextView mTextView = null;  
  28.     private ImageView mImageView = null;  
  29.       
  30.     public LevelMenuItem(Context context) {  
  31.         super(context);  
  32.     }  
  33.     public LevelMenuItem(Context context, AttributeSet attrs) {  
  34.         super(context, attrs);  
  35.           
  36.         LayoutInflater layoutInflater = (LayoutInflater) context.  
  37.                         getSystemService(Context.LAYOUT_INFLATER_SERVICE);  
  38.         layoutInflater.inflate(R.layout.level_menu_item, this);  
  39.           
  40.         TypedArray typedArray = context.obtainStyledAttributes(attrs  
  41.                 ,R.styleable.LevelMenuItem);  
  42.           
  43.         initWidget(typedArray);  
  44.     }  
  45.     private void initWidget(TypedArray typedArray)  
  46.     {  
  47.         mTextView = (TextView)findViewById(R.id.tv_item);  
  48.         String textString = typedArray.getString(R.styleable.LevelMenuItem_text);  
  49.         int textColor = typedArray.getColor(R.styleable.LevelMenuItem_text_color,  
  50.                 0xffffffff);  
  51.         float textSize = typedArray.getDimension(R.styleable.LevelMenuItem_text_size,  
  52.                 20);  
  53.         mTextView.setText(textString);  
  54.         mTextView.setTextColor(textColor);  
  55.         mTextView.setTextSize(textSize);  
  56.           
  57.         mImageView = (ImageView)findViewById(R.id.image_item);  
  58.         int imageHeight = (int) typedArray.getDimension(R.styleable.LevelMenuItem_image_height, 25);  
  59.         int imageWidth = (int) typedArray.getDimension(R.styleable.LevelMenuItem_image_width, 25);  
  60.         int imageSrc = typedArray.getResourceId(R.styleable.LevelMenuItem_image_src, 0);  
  61.         int imageBg = typedArray.getResourceId(R.styleable.LevelMenuItem_image_bg, 0);  
  62.         int imageAlpha = typedArray.getInt(R.styleable.LevelMenuItem_image_alpha, 255);  
  63.         mImageView.setAlpha(imageAlpha);  
  64.         mImageView.setImageResource(imageSrc);  
  65.         mImageView.setBackgroundResource(imageBg);  
  66.         LayoutParams layoutParams = new LayoutParams(imageWidth, imageHeight);  
  67.         mImageView.setLayoutParams(layoutParams);  
  68.           
  69.         typedArray.recycle();  
  70.     }  
  71.     /** 
  72.      * 设置此控件的文本 
  73.      * @param text 
  74.      */  
  75.     public void setText(String text)  
  76.     {  
  77.         mTextView.setText(text);  
  78.     }  
  79.     /** 
  80.      * 设置文字颜色 
  81.      * @param textColor 
  82.      */  
  83.     public void setTextColor(int textColor)  
  84.     {  
  85.         mTextView.setTextColor(textColor);  
  86.     }  
  87.     /** 
  88.      * 设置字体大小 
  89.      * @param textSize 
  90.      */  
  91.     public void setTextSize(int textSize)  
  92.     {  
  93.         mTextView.setTextSize(textSize);  
  94.     }  
  95.     /** 
  96.      * 设置图片 
  97.      * @param resId 
  98.      */  
  99.     public void setImageResource(int resId)  
  100.     {  
  101.         mImageView.setImageResource(resId);  
  102.     }  
  103.     /** 
  104.      * 设置图片背景 
  105.      */  
  106.     public void setBackgroundResource(int resId)  
  107.     {  
  108.         mImageView.setBackgroundResource(resId);  
  109.     }     
  110.     /** 
  111.      * 设置图片的不透名度 
  112.      * @param alpha 
  113.      */  
  114.     public void setImageAlpha(int alpha)  
  115.     {  
  116.         mImageView.setAlpha(alpha);  
  117.     }  
  118.     /** 
  119.      * 设置图片的大小 
  120.      * 这里面需要使用LayoutParams这个布局参数来设置 
  121.      * @param width 
  122.      * @param height 
  123.      */  
  124.     public void setImageSize(int width,int height)  
  125.     {  
  126.         LayoutParams layoutParams = new LayoutParams(width, height);  
  127.         mImageView.setLayoutParams(layoutParams);  
  128.     }  
  129.     /** 
  130.      * image点击事件的回调 
  131.      * @param listener 
  132.      */  
  133.     public void setOnClickListener(OnItemClickListener listener)  
  134.     {  
  135.         mImageView.setOnClickListener(new View.OnClickListener() {  
  136.             @Override  
  137.             public void onClick(View v) {  
  138.                 listener.onImageClick();  
  139.             }  
  140.         });  
  141.     }  
  142.     /** 
  143.      * 点击事件接口 
  144.      * @author linc 
  145.      * 
  146.      */  
  147.     public interface OnItemClickListener  
  148.     {  
  149.         public void onImageClick();  
  150.     }  
  151. }  
好,一个完整的组合控件就做好了,那么,我们如何使用呢?

我要在LevelMenu中用它。xml文件如下:

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     xmlns:linc="http://schemas.android.com/apk/res/com.linc.game"  
  4.     android:layout_width="fill_parent"  
  5.     android:layout_height="fill_parent"  
  6.     android:orientation="horizontal">      
  7.     <com.linc.game.LevelMenuItem  
  8.         android:id="@+id/item1"  
  9.         android:layout_width="70dp"  
  10.         android:layout_height="80dp"  
  11.         linc:text="@string/item1"  
  12.         linc:text_size="14sp"  
  13.         linc:text_color="#80fa8072"  
  14.         linc:image_src="@drawable/orange_button_selector"  
  15.         linc:image_alpha="128"  
  16.         linc:image_height="48dp"  
  17.         linc:image_width="48dp"  
  18.         />  
  19.     <com.linc.game.LevelMenuItem  
  20.         android:id="@+id/item2"  
  21.         android:layout_marginLeft="20dp"  
  22.         android:layout_width="70dp"  
  23.         android:layout_height="80dp"  
  24.         linc:text="@string/item2"  
  25.         linc:text_size="14sp"  
  26.         linc:text_color="#ffeee8aa"  
  27.         linc:image_src="@drawable/red_button_selector"  
  28.         linc:image_alpha="255"  
  29.         linc:image_height="48dp"  
  30.         linc:image_width="48dp"  
  31.         />     
  32.     <com.linc.game.LevelMenuItem  
  33.         android:id="@+id/item3"  
  34.         android:layout_marginLeft="20dp"  
  35.         android:layout_width="70dp"  
  36.         android:layout_height="80dp"  
  37.         linc:text="@string/item3"  
  38.         linc:text_size="14sp"  
  39.         linc:text_color="#80cd853f"  
  40.         linc:image_src="@drawable/yellow_button_selector"  
  41.         linc:image_alpha="128"  
  42.         linc:image_height="48dp"  
  43.         linc:image_width="48dp"  
  44.         />         
  45. </LinearLayout>  
linux
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

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