Android中自定义控件,三个构造函数

本文详细介绍了在Android应用开发中自定义控件的步骤与注意事项,包括类文件创建、属性定义、属性获取及使用。同时,强调了在自定义控件时抽象得彻底的重要性以及编写代码的严谨性,确保程序的稳定性和复用性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

自定义控件时,最好抽象得彻底,并且编写需严谨,因为可能程序中多处都会引用到它,或者提供给团队中的其他人使用。  

其一般步骤为:

1.创建控件的类文件,定义其功能逻辑。一般继承自现有控件或者View   

2.在res/values目录下创建attrs.xml文件,用于定义该控件的xml标签属性,方便在使用xml声明该控件时设置参数    

3.实现该控件的构造器,在构造器中把xml标签属性与后台代码中的变量相连接   

4.完成以上步骤之后,便可使用该控件  


需要注意的地方:  

一.View的三个构造函数       

第一个构造函数:     当不需要使用xml声明或者不需要使用inflate动态加载时候,实现此构造函数即可  

第二个构造函数:     当需要在xml中声明此控件,则需要实现此构造函数。并且在构造函数中把自定义的属性与控件的数据成员连接起来。  

第三个构造函数:     接受一个style资源  

二.View重要的回调  

  

onFinishInflate()     在此控件被通过xml声明的方式创建之后调用  

onMeasure(in,int)     计算本控件的宽高,如果继承自原有控件,则一般不需要重写此方法  

onLayout()     用于布局控件,对于不是继承ViewGroup的控件,一般不需要重写此方法  

onDraw()     在绘制控件时候调用,控件具体长什么样子就在此方法中实现  

三.设置自定义属性  

使用自定义控件时候,需要通过把xml中声明的属性与控件的数据成员连接起来  

在res/values目录下创建attrs.xml文件  

定义属性组  

在构造函数中获取这些值  


Example:  

1.myButton.java  

      
public class myButton extends Button
{
  private int step;
  
  public myButton(Context context, AttributeSet attrs, int defStyle)
  {
    super(context, attrs, defStyle);
    // TODO Auto-generated constructor stub
    init(attrs);
  }

  public myButton(Context context, AttributeSet attrs)
  {
    super(context, attrs);
    // TODO Auto-generated constructor stub
    init(attrs);
  }

  public myButton(Context context)
  {
    super(context);
    // TODO Auto-generated constructor stub
  }
  
  private void init(AttributeSet attrs)
  {
    if (attrs != null)
    {
      TypedArray a = getContext().obtainStyledAttributes(attrs,
          R.styleable.myButton);
      
       step = a.getInt(R.styleable.myButton_step, 0);
       
       a.recycle();
       
       this.setText(step+"");
    }
  }
}
myButton.java  

2.attrs.xml

      
<?xml version="1.0" encoding="utf-8"?>
<resources>
    <declare-styleable name="myButton">
        <attr name="step" format="integer"/>
    </declare-styleable>
</resources>
attrs.xml  

3.使用自定义控件

xmlns:views="http://schemas.android.com/apk/res/" + 子包名

      
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:tools="http://schemas.android.com/tools"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:paddingBottom="@dimen/activity_vertical_margin"
  android:paddingLeft="@dimen/activity_horizontal_margin"
  android:paddingRight="@dimen/activity_horizontal_margin"
  android:paddingTop="@dimen/activity_vertical_margin"
  android:orientation="vertical"
  tools:context=".MainActivity" 
  
  xmlns:views="http://schemas.android.com/apk/res/com.example.createvewtest">

  <TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/hello_world" />
  
  <com.example.views.myButton 
    android:id="@+id/btn"
    android:text="button"
    views:step="10"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"/>

</LinearLayout>

转载于:https://www.cnblogs.com/liuguanghai/p/3957330.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值