android 自定义控件 自定义属性详细介绍

在android相关应用开发过程中,固定的一些属性可能满足不了开发的需求,所以在一些特殊情况下,需要自定义控件与属性,本文将以此问题进行详细介绍,需要的朋友可以参考下

自定义控件在android中无处不见,自定义控件给了我们很大的方便。比如说,一个视图为imageview ,imagebutton ,textview 等诸多控件的组合,用的地方有很多,我们不可能每次都来写3个的组合,既浪费时间,效率又低。在这种情况下,我们就可以自定义一个view来替换他们,不仅提升了效率并且在xml中运用也是相当的美观。

一、控件自定义属性介绍 

以下示例中代码均在values/attrs.xml 中定义,属性均可随意命名。 
1. reference:参考某一资源ID。 
示例:
[html]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. <declare-styleable name="名称">  
  2.   <attr name="background" format="reference" />  
  3.   <attr name="src" format="reference" />  
  4. </declare-styleable>  

2. color:颜色值。 

示例:
[html]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. <declare-styleable name="名称">  
  2.   <attr name="textColor" format="color" />  
  3. </declare-styleable>  

3. boolean:布尔值。 

示例: 
[html]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. <declare-styleable name="名称">  
  2.   <attr name="focusable" format="boolean" />  
  3. </declare-styleable>  
4. dimension:尺寸值。 

示例:

[html]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. <declare-styleable name="名称">  
  2.   <attr name="layout_width" format="dimension" />  
  3. </declare-styleable>  
5. float:浮点值。 
示例: 
[html]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. <declare-styleable name="名称">  
  2.   <attr name="fromAlpha" format="float" />  
  3.   <attr name="toAlpha" format="float" />  
  4. </declare-styleable>  
6. integer:整型值。 
示例:
[html]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. <declare-styleable name="名称">  
  2.   <attr name="frameDuration" format="integer" />  
  3.   <attr name="framesCount" format="integer" />  
  4. </declare-styleable>  
7. string:字符串。 
示例:
[html]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. <declare-styleable name="名称">  
  2.   <attr name="text" format="string" />  
  3. </declare-styleable>  
8. fraction:百分数。 
示例:
[html]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. <declare-styleable name="名称">  
  2.   <attr name="pivotX" format="fraction" />  
  3.   <attr name="pivotY" format="fraction" />  
  4. </declare-styleable>  
9. enum:枚举值。 
示例: 
[html]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. <declare-styleable name="名称">  
  2.   <attr name="orientation">  
  3.     <enum name="horizontal" value="0" />  
  4.     <enum name="vertical" value="1" />  
  5.   </attr>  
  6. </declare-styleable>  
10. flag:位或运算。 
示例: 
[html]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. <declare-styleable name="名称">  
  2.   <attr name="windowSoftInputMode">  
  3.     <flag name="stateUnspecified" value="0" />  
  4.     <flag name="stateUnchanged" value="1" />  
  5.     <flag name="stateHidden" value="2" />  
  6.     <flag name="stateAlwaysHidden" value="3" />  
  7.   </attr>  
  8. </declare-styleable>  
11.多类型。 
示例:
[html]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. <declare-styleable name="名称">  
  2.   <attr name="background" format="reference|color" />  
  3. </declare-styleable>  

二、属性的使用以及自定义控件的实现 

1、构思控件的组成元素,思考所需自定义的属性。 
比如:我要做一个 <带阴影的按钮,按钮正下方有文字说明>(类似9宫格按钮) 
新建values/attrs.xml 
[html]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <resources>  
  3.   <declare-styleable name="custom_view">  
  4.     <attr name="custom_id" format="integer" />  
  5.     <attr name="src" format="reference" />  
  6.     <attr name="background" format="reference" />  
  7.     <attr name="text" format="string" />  
  8.     <attr name="textColor" format="color" />  
  9.     <attr name="textSize" format="dimension" />  
  10.   </declare-styleable>  
  11. </resources>  
以上,所定义为custom_view,custom_id为按钮id,src为按钮,background为阴影背景,text为按钮说明,textColor为字体颜色,textSize为字体大小。 
2、怎么自定义控件呢,怎么使用这些属性呢?话不多说请看代码,CustomView : 
[java]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. package com.nanlus.custom;  
  2. import com.nanlus.custom.R;  
  3. import android.content.Context;  
  4. import android.content.res.TypedArray;  
  5. import android.graphics.Color;  
  6. import android.graphics.drawable.Drawable;  
  7. import android.util.AttributeSet;  
  8. import android.view.Gravity;  
  9. import android.view.View;  
  10. import android.view.View.OnClickListener;  
  11. import android.widget.FrameLayout;  
  12. import android.widget.ImageButton;  
  13. import android.widget.ImageView;  
  14. import android.widget.TextView;  
  15. public class CustomView extends FrameLayout implements OnClickListener  
  16. {  
  17.     private CustomListener customListener = null;  
  18.     private Drawable mSrc = null, mBackground = null;  
  19.     private String mText = "";  
  20.     private int mTextColor = 0;  
  21.     private float mTextSize = 20;  
  22.     private int mCustomId = 0;  
  23.     private ImageView mBackgroundView = null;  
  24.     private ImageButton mButtonView = null;  
  25.     private TextView mTextView = null;  
  26.     private LayoutParams mParams = null;  
  27.     public CustomView(Context context)  
  28.     {  
  29.         super(context);  
  30.     }  
  31.     public CustomView(Context context, AttributeSet attrs)  
  32.     {  
  33.         super(context, attrs);  
  34.         TypedArray a = context.obtainStyledAttributes(attrs,  
  35.                        R.styleable.custom_view);  
  36.         mSrc = a.getDrawable(R.styleable.custom_view_src);  
  37.         mBackground = a.getDrawable(R.styleable.custom_view_background);  
  38.         mText = a.getString(R.styleable.custom_view_text);  
  39.         mTextColor = a.getColor(R.styleable.custom_view_textColor,  
  40.                                 Color.WHITE);  
  41.         mTextSize = a.getDimension(R.styleable.custom_view_textSize, 20);  
  42.         mCustomId = a.getInt(R.styleable.custom_view_custom_id, 0);  
  43.         mTextView = new TextView(context);  
  44.         mTextView.setTextSize(mTextSize);  
  45.         mTextView.setTextColor(mTextColor);  
  46.         mTextView.setText(mText);  
  47.         mTextView.setGravity(Gravity.CENTER);  
  48.         mTextView.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,  
  49.                                   LayoutParams.WRAP_CONTENT));  
  50.         mButtonView = new ImageButton(context);  
  51.         mButtonView.setImageDrawable(mSrc);  
  52.         mButtonView.setBackgroundDrawable(null);  
  53.         mButtonView.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,  
  54.                                     LayoutParams.WRAP_CONTENT));  
  55.         mButtonView.setOnClickListener(this);  
  56.         mBackgroundView = new ImageView(context);  
  57.         mBackgroundView.setImageDrawable(mBackground);  
  58.         mBackgroundView.setLayoutParams(new LayoutParams(  
  59.                                             LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));  
  60.         addView(mBackgroundView);  
  61.         addView(mButtonView);  
  62.         addView(mTextView);  
  63.         this.setOnClickListener(this);  
  64.         a.recycle();  
  65.     }  
  66.     @Override  
  67.     protected void onAttachedToWindow()  
  68.     {  
  69.         super.onAttachedToWindow();  
  70.         mParams = (LayoutParams) mButtonView.getLayoutParams();  
  71.         if (mParams != null)  
  72.         {  
  73.             mParams.gravity = Gravity.CENTER_HORIZONTAL | Gravity.TOP;  
  74.             mButtonView.setLayoutParams(mParams);  
  75.         }  
  76.         mParams = (LayoutParams) mBackgroundView.getLayoutParams();  
  77.         if (mParams != null)  
  78.         {  
  79.             mParams.gravity = Gravity.CENTER_HORIZONTAL | Gravity.TOP;  
  80.             mBackgroundView.setLayoutParams(mParams);  
  81.         }  
  82.         mParams = (LayoutParams) mTextView.getLayoutParams();  
  83.         if (mParams != null)  
  84.         {  
  85.             mParams.gravity = Gravity.CENTER_HORIZONTAL | Gravity.BOTTOM;  
  86.             mTextView.setLayoutParams(mParams);  
  87.         }  
  88.     }  
  89.     public void setCustomListener(CustomListener l)  
  90.     {  
  91.         customListener = l;  
  92.     }  
  93.     @Override  
  94.     public void onClick(View v)  
  95.     {  
  96.         if (customListener != null)  
  97.         {  
  98.             customListener.onCuscomClick(v, mCustomId);  
  99.         }  
  100.     }  
  101.     public interface CustomListener  
  102.     {  
  103.         void onCuscomClick(View v, int custom_id);  
  104.     }  
  105. }  
代码很简单,就不多说,下面来看看我们的CustomView是怎么用的,请看: 
3、自定义控件的使用 
话不多说,请看代码,main.xml: 
[html]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. <?xml version="1.0" encoding="utf-8"?>   
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"   
  3. xmlns:nanlus="http://schemas.android.com/apk/res/com.nanlus.custom"   
  4. android:layout_width="fill_parent"   
  5. android:layout_height="fill_parent" >   
  6. <LinearLayout   
  7. android:layout_width="wrap_content"   
  8. android:layout_height="wrap_content"   
  9. android:layout_centerHorizontal="true"   
  10. android:layout_centerVertical="true"   
  11. android:orientation="horizontal" >   
  12. <com.nanlus.custom.CustomView   
  13. android:id="@+id/custom1"   
  14. android:layout_width="wrap_content"   
  15. android:layout_height="wrap_content"   
  16. android:layout_weight="1"   
  17. nanlus:background="@drawable/background"   
  18. nanlus:custom_id="1"   
  19. nanlus:src="@drawable/style_button"   
  20. nanlus:text="按钮1" >   
  21. </com.nanlus.custom.CustomView>   
  22. </LinearLayout>   
  23. </RelativeLayout>   
在这里需要解释一下, 
xmlns:nanlus="http://schemas.android.com/apk/res/com.nanlus.custom" 
nanlus为在xml中的前缀,com.nanlus.custom为包名 
4、在Activity中,直接上代码
[java]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. package com.nanlus.custom;  
  2. import android.os.Bundle;  
  3. import android.view.View;  
  4. import android.widget.ImageButton;  
  5. import android.widget.ImageView;  
  6. import android.widget.TextView;  
  7. import android.widget.Toast;  
  8. import com.nanlus.BaseActivity;  
  9. import com.nanlus.custom.R;  
  10. import com.nanlus.custom.CustomView.CustomListener;  
  11. public class CustomActivity extends BaseActivity implements CustomListener  
  12. {  
  13.     @Override  
  14.     protected void onCreate(Bundle savedInstanceState)  
  15.     {  
  16.         super.onCreate(savedInstanceState);  
  17.         setContentView(R.layout.main);  
  18.         ((CustomView) this.findViewById(R.id.custom1)).setCustomListener(this);  
  19.     }  
  20.     @Override  
  21.     public void onCuscomClick(View v, int custom_id)  
  22.     {  
  23.         switch (custom_id)  
  24.         {  
  25.         case 1:  
  26.             Toast.makeText(this"hello !!!", Toast.LENGTH_LONG).show();  
  27.             break;  
  28.         default:  
  29.             break;  
  30.         }  
  31.     }  
  32. }  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值