一、通过View实现自定义视图

通过构造函数创建可视界面

public class MyView extends View {

// Constructor required for in-code creation
  public MyView(Context context) {
    super(context);
  }

// Constructor required for inflation from resource file
  public MyView (Context context, AttributeSet ats, int defaultStyle) {
    super(context, ats, defaultStyle );
  }

//Constructor required for inflation from resource file
  public MyView (Context context, AttributeSet attrs) {
    super(context, attrs);
  }

通过onDraw()绘制控件。(注意其参数为一个Canvas对象)

@Override
protected void onDraw(Canvas canvas) {
   // Get the size of the control based on the last call to onMeasure.
   int height = getMeasuredHeight();
   int width = getMeasuredWidth();

// Find the center
   int px = width/2;
   int py = height/2;

// Create the new paint brushes.
   // NOTE: For efficiency this should be done in
   // the views's constructor
   Paint mTextPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
   mTextPaint.setColor(Color.WHITE);

// Define the string.
   String displayText = "Hello World!";

// Measure the width of the text string.
   float textWidth = mTextPaint.measureText(displayText);

// Draw the text string in the center of the control.
   canvas.drawText(displayText, px-textWidth/2, py, mTextPaint);
}

通过onMeasure()调整控件大小

@Override

protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
   int measuredHeight = measureHeight(heightMeasureSpec);
   int measuredWidth = measureWidth(widthMeasureSpec);

setMeasuredDimension(measuredHeight, measuredWidth);
}

private int measureHeight(int measureSpec) {
   int specMode = MeasureSpec.getMode(measureSpec);
   int specSize = MeasureSpec.getSize(measureSpec);

//  Default size if no limits are specified.
   int result = 500;

if (specMode == MeasureSpec.AT_MOST) {
     // Calculate the ideal size of your
     // control within this maximum size.
     // If your control fills the available
     // space return the outer bound.
     result = specSize;
   } else if (specMode == MeasureSpec.EXACTLY) {
     // If your control can fit within these bounds return that value.
     result = specSize;
   }
   return result;
}

处理用户交互事件

使用自定义控件

<com.paad.compass.CompassView/>

二、Adapter简介

Adapter实现了数据绑定到扩展了AdapterView的试图组GroupView.Adapter负责创建被绑定groupView的子视图。

public class MyArrayAdapter extends ArrayAdapter<MyClass> {

int resource;

public MyArrayAdapter(Context context,
                         int _resource,
                         List<MyClass> items) {
    super(context, _resource, items);
    resource = _resource;
  }

ArrayAdapter<MyClass>中的MyClass和构造函数的泛型的类型一致。

Context通常是被绑定的对象,_resource是子视图。items是将要用到的泛型数据。

另外一个比较主要的是getView覆盖方法

public View getView(int position, View convertView, ViewGroup parent) {
   LinearLayout todoView;//第n个项的载体

ToDoItem item = getItem(position);//得到第n个项

String taskString = item.getTask();
   Date createdDate = item.getCreated();
   SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yy");
   String dateString = sdf.format(createdDate);

if (convertView == null) {
     todoView = new LinearLayout(getContext());
     String inflater = Context.LAYOUT_INFLATER_SERVICE;
     LayoutInflater li;
     li = (LayoutInflater)getContext().getSystemService(inflater);
     li.inflate(resource, todoView, true);
   } else {
     todoView = (LinearLayout) convertView;
   }

TextView dateView = (TextView)todoView.findViewById(R.id.rowDate);
   TextView taskView = (TextView)todoView.findViewById(R.id.row);

dateView.setText(dateString);
   taskView.setText(taskString);

return todoView;
}

具体的LinearLayout 还有待考证

andriod自定义视图的更多相关文章

  1. 《连载 | 物联网框架ServerSuperIO教程》- 13.自定义视图显示接口开发,满足不同的显示需求

    1.C#跨平台物联网通讯框架ServerSuperIO(SSIO)介绍 <连载 | 物联网框架ServerSuperIO教程>1.4种通讯模式机制. <连载 | 物联网框架Serve ...

  2. Android中的自定义视图控件

    简介 当现有控件不能满足需求时,就需要自定义控件. 自定义控件属性 自定义控件首先要继承自View,重写两个构造函数. 第一个是代码中使用的: public MyRect(Context contex ...

  3. 自定义视图引擎,实现MVC主题快速切换

    一个网站的主题包括布局,色调,内容展示等,每种主题在某些方面应该或多或少不一样的,否则就不能称之为不同的主题了.每一个网站至少都有一个主题,我这里称之为默认主题,也就是我们平常开发设计网站时的一个固定 ...

  4. SpringMVC自定义视图 Excel视图和PDF视图

    一.自定义视图-Excel视图 1.Maven依赖 引入POI <dependency> <groupId>org.apache.poi</groupId> < ...

  5. MVC自定义视图规则

    自定义规则: using System.Web.Mvc; using System.Configuration; namespace Research { public class ViewConfi ...

  6. (翻译)为你的MVC应用程序创建自定义视图引擎

    Creating your own MVC View Engine For MVC Application 原文链接:http://www.codeproject.com/Articles/29429 ...

  7. iOS-xib(使用XIB实现嵌套自定义视图)

    参考:http://wtlucky.github.io/geekerprobe/blog/2014/08/10/nested-xib-views/?utm_source=tuicool 因为主要练习x ...

  8. 自定义视图一:扩展现有的视图,添加新的XML属性

    这个系列是老外写的,干货!翻译出来一起学习.如有不妥,不吝赐教! 简介 这个系列详细的介绍了如何穿件Android自定义视图.主要涉及的内容有如何绘制内容,layout和measure的原理,如何继承 ...

  9. 一个基于MBProgressHUD的自定义视图hud例子

    项目中用到的一个hud,基于MBProgressHUD,使用自定义视图实现的,动画效果是从网上参考的,并不是很理想.有需要的可以看看,这里是源码(源码用了cocoapods,运行前需要pod inst ...

随机推荐

  1. top 学习

    通常top命令是会持续运行而不终止的. 要在脚本里用,需要添加一些选项参数,尤其是-b.例如:top -b -n 2 -d 3 >/tmp/log -b表示批处理模式(Batch mode),以 ...

  2. 实现一个简单的android开关

    近期在学习android中的graphics中绘图系列.依照大神思路.找葫芦画瓢实现了一个开关.如图下: 记录一下实现方式: 1.画背景 上图形状.分成两个半圆与一个矩形,那么代码能够写成: priv ...

  3. TCP网络编程杂谈

    作为一名IT工程师,网络通信编程相信都会接触到,比如Web开发的HTTP库,Java中的Netty,或者C/C++中的Libevent,Libev等第三方通信库,甚至是直接使用Socket API,但 ...

  4. SQL 中的 UNION 和UNION ALL 的区别

    UNION表示“并”,当用的时候,系统会自动将重复的元组去掉,如果要保留重复元组则就用UNION ALL UNION 会合并重复数据,(由于要合并重复,该操所 隐藏着一个 排序的操作.)UNION A ...

  5. 大家来找茬:富连网今天中午抢购二手iPhone时网站无法访问的问题

    前几天在新闻区看到富士康卖二手iPhone的新闻,今天又看到说今天中午12点开抢.一大早就发现富连网无法访问了.前几天刚看到新闻的时候注册了个账号进去看了看,发现页面加载速度非常慢,今天中午基本无法打 ...

  6. javascript 20个正则表达式

    正则表达式,一个十分古老而又强大的文本处理工具,仅仅用一段非常简短的表达式语句,便能够快速实现一个非常复杂的业务逻辑.熟练地掌握正则表达式的话,能够使你的开发效率得到极大的提升. 正则表达式经常被用于 ...

  7. Node.js中,获取req请求的原始IP

    Node.js代码 var express = require('express'); var app = express(); var http = require('http'); var ser ...

  8. php分享二十四:数组

    1:isset() 对于数组中为 NULL 的值不会返回 TRUE,而 array_key_exists() 会. 2:利用array_filter和strlen快速过滤数组中等于0的值 $path ...

  9. Spring Security教程(四):自定义登录页

    在前面的例子中,登陆页面都是用的Spring Security自己提供的,这明显不符合实际开发场景,同时也没有退出和注销按钮,因此在每次测试的时候都要通过关闭浏览器来注销达到清除session的效果. ...

  10. Spring ORM数据訪问——Hibernate

    Hibernate 我们将首先介绍Spring环境中的Hibernate 5.然后介绍使用Hibernate 5来演示Spring集成O-R映射器的方法. 本节将具体介绍很多问题,并显示DAO实现和事 ...