自定义控件之二 attr属性值

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

一、控件自定义属性介绍

以下示例中代码均在values/attrs.xml 中定义,属性均可随意命名。

1. reference:参考某一资源ID。

示例:
[java]  view plain  copy
  1. <declare-styleable name = "名称">  
  2.        <attr name = "background" format = "reference" />  
  3.        <attr name = "src" format = "reference" />  
  4. </declare-styleable>  
2. color:颜色值。
示例:
[java]  view plain  copy
  1. <declare-styleable name = "名称">  
  2.        <attr name = "textColor" format = "color" />  
  3. </declare-styleable>  
3. boolean:布尔值。
示例:
[java]  view plain  copy
  1. <declare-styleable name = "名称">  
  2.        <attr name = "focusable" format = "boolean" />  
  3. </declare-styleable>  
4. dimension:尺寸值。
示例:
[java]  view plain  copy
  1. <declare-styleable name = "名称">  
  2.        <attr name = "layout_width" format = "dimension" />  
  3. </declare-styleable>  
5. float:浮点值。
示例:
[java]  view plain  copy
  1. <declare-styleable name = "名称">  
  2.        <attr name = "fromAlpha" format = "float" />  
  3.        <attr name = "toAlpha" format = "float" />  
  4. </declare-styleable>  
6. integer:整型值。
示例:
[java]  view plain  copy
  1. <declare-styleable name = "名称">  
  2.        <attr name = "frameDuration" format="integer" />  
  3.        <attr name = "framesCount" format="integer" />  
  4. </declare-styleable>  
7. string:字符串。
示例:
[java]  view plain  copy
  1. <declare-styleable name = "名称">  
  2.        <attr name = "text" format = "string" />  
  3. </declare-styleable>  
8. fraction:百分数。
示例:
[java]  view plain  copy
  1. <declare-styleable name="名称">  
  2.        <attr name = "pivotX" format = "fraction" />  
  3.        <attr name = "pivotY" format = "fraction" />  
  4. </declare-styleable>  
9. enum:枚举值。
示例:
[java]  view plain  copy
  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:位或运算。

示例:
[java]  view plain  copy
  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.多类型。
示例:
[java]  view plain  copy
  1. <declare-styleable name = "名称">  
  2.        <attr name = "background" format = "reference|color" />  
  3. </declare-styleable>  

-------------------------------------------------------------------------------------------

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

1、构思控件的组成元素,思考所需自定义的属性。

       比如:我要做一个  <带阴影的按钮,按钮正下方有文字说明>(类似9宫格按钮) 

       新建values/attrs.xml

[java]  view plain  copy
  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
  1. package com.nanlus.custom;  
  2.   
  3. import com.nanlus.custom.R;  
  4. import android.content.Context;  
  5. import android.content.res.TypedArray;  
  6. import android.graphics.Color;  
  7. import android.graphics.drawable.Drawable;  
  8. import android.util.AttributeSet;  
  9. import android.view.Gravity;  
  10. import android.view.View;  
  11. import android.view.View.OnClickListener;  
  12. import android.widget.FrameLayout;  
  13. import android.widget.ImageButton;  
  14. import android.widget.ImageView;  
  15. import android.widget.TextView;  
  16. public class CustomView extends FrameLayout implements OnClickListener {  
  17.   
  18.     private CustomListener customListener = null;  
  19.   
  20.     private Drawable mSrc = null, mBackground = null;  
  21.     private String mText = "";  
  22.     private int mTextColor = 0;  
  23.     private float mTextSize = 20;  
  24.     private int mCustomId = 0;  
  25.   
  26.     private ImageView mBackgroundView = null;  
  27.     private ImageButton mButtonView = null;  
  28.     private TextView mTextView = null;  
  29.   
  30.     private LayoutParams mParams = null;  
  31.   
  32.     public CustomView(Context context) {  
  33.         super(context);  
  34.   
  35.     }  
  36.   
  37.     public CustomView(Context context, AttributeSet attrs) {  
  38.         super(context, attrs);  
  39.   
  40.         TypedArray a = context.obtainStyledAttributes(attrs,  
  41.                 R.styleable.custom_view);  
  42.   
  43.         mSrc = a.getDrawable(R.styleable.custom_view_src);  
  44.         mBackground = a.getDrawable(R.styleable.custom_view_background);  
  45.         mText = a.getString(R.styleable.custom_view_text);  
  46.         mTextColor = a.getColor(R.styleable.custom_view_textColor,  
  47.                 Color.WHITE);  
  48.         mTextSize = a.getDimension(R.styleable.custom_view_textSize, 20);  
  49.         mCustomId = a.getInt(R.styleable.custom_view_custom_id, 0);  
  50.   
  51.         mTextView = new TextView(context);  
  52.         mTextView.setTextSize(mTextSize);  
  53.         mTextView.setTextColor(mTextColor);  
  54.         mTextView.setText(mText);  
  55.         mTextView.setGravity(Gravity.CENTER);  
  56.         mTextView.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,  
  57.                 LayoutParams.WRAP_CONTENT));  
  58.   
  59.         mButtonView = new ImageButton(context);  
  60.         mButtonView.setImageDrawable(mSrc);  
  61.         mButtonView.setBackgroundDrawable(null);  
  62.         mButtonView.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,  
  63.                 LayoutParams.WRAP_CONTENT));  
  64.         mButtonView.setOnClickListener(this);  
  65.   
  66.         mBackgroundView = new ImageView(context);  
  67.         mBackgroundView.setImageDrawable(mBackground);  
  68.         mBackgroundView.setLayoutParams(new LayoutParams(  
  69.                 LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));  
  70.   
  71.         addView(mBackgroundView);  
  72.         addView(mButtonView);  
  73.         addView(mTextView);  
  74.   
  75.         this.setOnClickListener(this);  
  76.   
  77.         a.recycle();  
  78.     }  
  79.   
  80.     @Override  
  81.     protected void onAttachedToWindow() {  
  82.         super.onAttachedToWindow();  
  83.   
  84.         mParams = (LayoutParams) mButtonView.getLayoutParams();  
  85.         if (mParams != null) {  
  86.             mParams.gravity = Gravity.CENTER_HORIZONTAL | Gravity.TOP;  
  87.             mButtonView.setLayoutParams(mParams);  
  88.         }  
  89.   
  90.         mParams = (LayoutParams) mBackgroundView.getLayoutParams();  
  91.         if (mParams != null) {  
  92.             mParams.gravity = Gravity.CENTER_HORIZONTAL | Gravity.TOP;  
  93.             mBackgroundView.setLayoutParams(mParams);  
  94.         }  
  95.   
  96.         mParams = (LayoutParams) mTextView.getLayoutParams();  
  97.         if (mParams != null) {  
  98.             mParams.gravity = Gravity.CENTER_HORIZONTAL | Gravity.BOTTOM;  
  99.             mTextView.setLayoutParams(mParams);  
  100.         }  
  101.     }  
  102.   
  103.     public void setCustomListener(CustomListener l) {  
  104.         customListener = l;  
  105.     }  
  106.   
  107.     @Override  
  108.     public void onClick(View v) {  
  109.         if (customListener != null) {  
  110.             customListener.onCuscomClick(v, mCustomId);  
  111.         }  
  112.     }  
  113.   
  114.     public interface CustomListener {  
  115.         void onCuscomClick(View v, int custom_id);  
  116.     }  
  117. }  

代码很简单,就不多说,下面来看看我们的CustomView是怎么用的,请看:

 
3、自定义控件的使用

      话不多说,请看代码,main.xml:

[java]  view plain  copy
  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.   
  7.     <LinearLayout  
  8.         android:layout_width="wrap_content"  
  9.         android:layout_height="wrap_content"  
  10.         android:layout_centerHorizontal="true"  
  11.          android:layout_centerVertical="true"  
  12.         android:orientation="horizontal" >  
  13.   
  14.         <com.nanlus.custom.CustomView  
  15.             android:id="@+id/custom1"  
  16.             android:layout_width="wrap_content"  
  17.             android:layout_height="wrap_content"  
  18.             android:layout_weight="1"  
  19.             nanlus:background="@drawable/background"  
  20.             nanlus:custom_id="1"  
  21.             nanlus:src="@drawable/style_button"  
  22.             nanlus:text="按钮1" >  
  23.         </com.nanlus.custom.CustomView>  
  24.     </LinearLayout>  
  25.   
  26.   
  27. </RelativeLayout>  

在这里需要解释一下,

xmlns:nanlus="http://schemas.android.com/apk/res/com.nanlus.custom"

nanlus为在xml中的前缀,com.nanlus.custom为包名

4、在Activity中,直接上代码

[java]  view plain  copy
  1. package com.nanlus.custom;  
  2.   
  3. import android.os.Bundle;  
  4. import android.view.View;  
  5. import android.widget.ImageButton;  
  6. import android.widget.ImageView;  
  7. import android.widget.TextView;  
  8. import android.widget.Toast;  
  9. import com.nanlus.BaseActivity;  
  10. import com.nanlus.custom.R;  
  11. import com.nanlus.custom.CustomView.CustomListener;  
  12.   
  13. public class CustomActivity extends BaseActivity implements CustomListener {  
  14.   
  15.     @Override  
  16.     protected void onCreate(Bundle savedInstanceState) {  
  17.         super.onCreate(savedInstanceState);  
  18.   
  19.         setContentView(R.layout.main);  
  20.   
  21.         ((CustomView) this.findViewById(R.id.custom1)).setCustomListener(this);  
  22.     }  
  23.   
  24.     @Override  
  25.     public void onCuscomClick(View v, int custom_id) {  
  26.         switch (custom_id) {  
  27.         case 1:  
  28.             Toast.makeText(this"hello !!!", Toast.LENGTH_LONG).show();  
  29.             break;  
  30.         default:  
  31.             break;  
  32.         }  
  33.   
  34.     }  
  35.   
  36. }  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值