Android自定义控件

Android自定义控件

我们所用的所有控件都是直接或者间接继承自View的,所用的布局都是直接或间接继承自ViewGroup的。View是Android中一种最基本的UI组件,它可以在屏幕上绘制一块矩形区域,并能响应这块区域的各种事件,因此,我们使用的各种控件其实就是在View的基础上又添加了各自特有的功能。

如果系统自带的控件并不能满足我们的需求时,可以利用上面的继承结构来创建自定义控件。自定义控件有几种方式:

1.       引入布局

一般我们的程序中可能要多个相同的标题栏,如果在每个布局中都写一遍同样的标题栏代码,明显会导致代码的大量重复。这时我们就可以使用引用布局的方式来解决。


新建一个布局title.xml

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

    android:layout_width="match_parent"

    android:layout_height="wrap_content"

    android:background="@drawable/title_bg">

   

    <Button android:id="@+id/title_back"

       android:layout_width="wrap_content"

       android:layout_height="wrap_content"

       android:layout_gravity="center"

       android:layout_margin="5dip"

       android:background="@drawable/back_bg"/>

   

    <TextView android:id="@+id/title_text"

       android:layout_width="0dip"

       android:layout_height="wrap_content"

       android:layout_gravity="center"

       android:layout_weight="1"

       android:gravity="center"

       android:text="Title Text"

       android:textSize="24sp"/>

   

    <Button android:id="@+id/title_edit"

       android:layout_width="wrap_content"

       android:layout_height="wrap_content"

       android:layout_gravity="center"

       android:layout_margin="5dip"

       android:background="@drawable/edit_bg"/>

</LinearLayout>

在activity.xml中添加如下代码:

<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">  

   

    <include layout="@layout/title"/>

  

</LinearLayout>

使用这种方式,不管有多少布局需要添加标题栏,只需一行include语句就可以了。

2.       自定义控件

引入布局的技巧确实解决了重复编写布局代码的问题,但是如果布局中有一些控件要求能够响应事件,我们还是需要在每个活动中为这些控件单独编写一次事件注册的代码。比如说标题栏中的返回按钮,其实不管是在哪一个活动中,这个按钮的功能都是相同的,即销毁掉当前活动。而如果在每一个活动中都需要重新注册一遍返回按钮的点击事件,无疑又是增加了很多重复代码,这种情况最好是使用自定义控件的方式来解决。

自定义控件可以通过继承View的方式或者继承布局(LinearLayout等)方式。       

2.1        view

View定义了绘图的基本操作,由三个函数完成:measure()、layout()、draw(),其内部又分别包含了onMeasure()、onLayout()、onDraw()三个子方法。


package com.jsr.customizeview;

 

import java.io.InputStream;

 

import android.content.Context;

import android.graphics.Bitmap;

import android.graphics.BitmapFactory;

import android.graphics.Canvas;

import android.graphics.Color;

import android.graphics.Paint;

import android.util.AttributeSet;

import android.view.View;

/**

 * 自定义View

 *@author 20150701123

 *

 */

public class MyView extends View{

    private String mtext;

    private int msrc;

    public MyView(Context context, AttributeSetattrs) {

       super(context, attrs);

       // TODO Auto-generated constructor stub

       /**

        * 通过构造函数中引入的AttributeSet去查找xml布局的属性名称,然后找到它对应引用的资源ID去找值

        */

       int textId = attrs.getAttributeResourceValue(null, "Text", 0);

       int srcId = attrs.getAttributeResourceValue(null, "Src", 0);

       mtext = context.getResources().getText(textId).toString();

       msrc = srcId;

    }

   @Override

    protected void onDraw(Canvas canvas) {

       // TODO Auto-generated method stub

       Paint paint = new Paint();

       paint.setColor(Color.RED);

       InputStream is = getResources().openRawResource(msrc);

       Bitmap mBitmap = BitmapFactory.decodeStream(is);

       int bh = mBitmap.getHeight();

       int bw = mBitmap.getWidth();

       //绘制

       canvas.drawBitmap(mBitmap, 0, 0, paint);

       canvas.drawText(mtext, bw/4, 80, paint);

    }

 

}

布局文件

<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"

    >

 

    <com.jsr.customizeview.MyView

       android:id="@+id/myView1"

       android:layout_width="wrap_content"

       android:layout_height="wrap_content"

       Text="@string/helloworld"

       Src="@drawable/xh"/>

</LinearLayout>

2.2        重写LinearLayout中两个参数的构造方法

package com.jsr.customizeview;

 

import android.content.Context;

import android.util.AttributeSet;

import android.view.LayoutInflater;

import android.view.View;

import android.widget.Button;

import android.widget.LinearLayout;

import android.widget.Toast;

 

public class Titlelayout extends LinearLayout{

 

    public Titlelayout(Context context, AttributeSetattrs) {

       super(context, attrs);

       // TODO Auto-generated constructor stub

       LayoutInflater.from(context).inflate(R.layout.title, this,true);

       Button titleBack = (Button)findViewById(R.id.title_back);

       Button titleEdit = (Button)findViewById(R.id.title_edit);

       titleBack.setOnClickListener(new OnClickListener() {

           

           @Override

           public void onClick(View v) {

                // TODOAuto-generated method stub

                Toast.makeText(getContext(), "You clickedback button", Toast.LENGTH_SHORT).show();

           }

       });

       titleEdit.setOnClickListener(new OnClickListener() {

           

           @Override

           public void onClick(View v) {

                // TODOAuto-generated method stub

                Toast.makeText(getContext(), "Yout clickededit button", Toast.LENGTH_SHORT).show();

           }

       });

   }

 

}

在布局文件中添加这个自定义控件

<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"

    >

 

    <com.jsr.customizeview.Titlelayout

       android:layout_width="wrap_content"

       android:layout_height="wrap_content"/>

   

  

</LinearLayout>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值