继承View,重写构造函数、onDraw,(onMeasure)等函数。
如果自定义的View需要有自定义的属性,需要在values下建立attrs.xml。在其中定义你的属性。
在使用到自定义View的xml布局文件中需要加入xmlns:前缀="http://schemas.android.com/apk/res/你的自定义View所在的包路径".
在使用自定义属性的时候,使用前缀:属性名,如my:textColor="#FFFFFFF"。
先预览一下效果图吧
![](http://dl.iteye.com/upload/attachment/0082/3719/415a117b-32fc-3db4-ae06-bc1f7ad40646.png)
自定义控件布局文件(实现左图片,右文字)
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity" > <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/iv" android:src="@drawable/save_picker" android:paddingTop="5dip" android:paddingBottom="5dip" android:paddingLeft="40dip" android:layout_gravity="center_vertical" android:contentDescription="@string/app_name"/> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/sure" android:textColor="#000000" android:id="@+id/tv" android:layout_marginLeft="8dip" android:layout_gravity="center_vertical"/> </LinearLayout>
自定义控件的主类,继承LinearLayout
import android.content.Context; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; import android.util.AttributeSet; import android.view.LayoutInflater; import android.widget.ImageView; import android.widget.LinearLayout; import android.widget.TextView; public class MyView extends LinearLayout{ private ImageView iv; private TextView tv; public MyView(Context context) { super(context); } public MyView(Context context, AttributeSet attrs) { super(context, attrs); LayoutInflater.from(context).inflate(R.layout.activity_main, this,true); iv = (ImageView)findViewById(R.id.iv); tv = (TextView)findViewById(R.id.tv); } @Override protected void onDraw(Canvas canvas) { Paint paint = new Paint(); paint.setColor(Color.RED); canvas.drawLine(200, 200, 400, 400, paint); System.out.println("===========paint============="); super.onDraw(canvas); } /** * 设置图片资源 */ public void setImageResource(int resId){ iv.setImageResource(resId); } /** * 设置显示的文字 */ public void setTextViewText(String text){ tv.setText(text); } }
login.xml 主activity加载的布局文件
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="horizontal" android:layout_gravity="bottom" > <com.zdybutton.example.MyView android:id="@+id/bt_confirm" android:layout_height="wrap_content" android:layout_width="wrap_content" android:layout_alignParentBottom="true" android:background="@drawable/acm_bg_btnblue_nor" android:clickable="true" android:focusable="true" /> <com.zdybutton.example.MyView android:id="@+id/bt_cancel" android:layout_toRightOf="@id/bt_confirm" android:layout_height="wrap_content" android:layout_width="wrap_content" android:layout_alignParentBottom="true" android:background="@drawable/acm_bg_btnblue_nor" android:clickable="true" android:focusable="true" /> </RelativeLayout>
MainActivity
package com.zdybutton.example; import android.app.Activity; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Toast; public class MainActivity extends Activity { private MyView view1; private MyView view2; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.login); view1 = (MyView)findViewById(R.id.bt_confirm); view2 = (MyView)findViewById(R.id.bt_cancel); view1.setTextViewText("确定"); view1.setImageResource(R.drawable.save_picker); view2.setTextViewText("取消"); view2.setImageResource(R.drawable.cancle_picker); view1.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { Toast.makeText(MainActivity.this, "点击确定", Toast.LENGTH_SHORT).show(); } }); } }
项目详见附件......