android自定义控件入门

Android自定义View实现很简单
继承View,重写构造函数、onDraw,(onMeasure)等函数。
如果自定义的View需要有自定义的属性,需要在values下建立attrs.xml。在其中定义你的属性。
在使用到自定义View的xml布局文件中需要加入xmlns:前缀="http://schemas.android.com/apk/res/你的自定义View所在的包路径".
在使用自定义属性的时候,使用前缀:属性名,如my:textColor="#FFFFFFF"。

[color=red]先预览一下效果图吧[/color]
[img]http://dl.iteye.com/upload/attachment/0082/3719/415a117b-32fc-3db4-ae06-bc1f7ad40646.png[/img]

自定义控件布局文件(实现左图片,右文字)

<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();
}
});
}
}



项目详见附件......
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
【1】项目代码完整且功能都验证ok,确保稳定可靠运行后才上传。欢迎下载使用!在使用过程中,如有问题或建议,请及时私信沟通,帮助解答。 【2】项目主要针对各个计算机相关专业,包括计科、信息安全、数据科学与大数据技术、人工智能、通信、物联网等领域的在校学生、专业教师或企业员工使用。 【3】项目具有较高的学习借鉴价值,不仅适用于小白学习入门进阶。也可作为毕设项目、课程设计、大作业、初期项目立项演示等。 【4】如果基础还行,或热爱钻研,可基于此项目进行二次开发,DIY其他不同功能,欢迎交流学习。 【注意】 项目下载解压后,项目名字和项目路径不要用中文,否则可能会出现解析不了的错误,建议解压重命名为英文名字后再运行!有问题私信沟通,祝顺利! 基于C语言实现智能决策的人机跳棋对战系统源码+报告+详细说明.zip基于C语言实现智能决策的人机跳棋对战系统源码+报告+详细说明.zip基于C语言实现智能决策的人机跳棋对战系统源码+报告+详细说明.zip基于C语言实现智能决策的人机跳棋对战系统源码+报告+详细说明.zip基于C语言实现智能决策的人机跳棋对战系统源码+报告+详细说明.zip基于C语言实现智能决策的人机跳棋对战系统源码+报告+详细说明.zip基于C语言实现智能决策的人机跳棋对战系统源码+报告+详细说明.zip基于C语言实现智能决策的人机跳棋对战系统源码+报告+详细说明.zip基于C语言实现智能决策的人机跳棋对战系统源码+报告+详细说明.zip基于C语言实现智能决策的人机跳棋对战系统源码+报告+详细说明.zip基于C语言实现智能决策的人机跳棋对战系统源码+报告+详细说明.zip基于C语言实现智能决策的人机跳棋对战系统源码+报告+详细说明.zip基于C语言实现智能决策的人机跳棋对战系统源码+报告+详细说明.zip
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值