博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
andriod自定义视图
阅读量:6623 次
发布时间:2019-06-25

本文共 3322 字,大约阅读时间需要 11 分钟。

一、通过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 还有待考证

转载地址:http://ectpo.baihongyu.com/

你可能感兴趣的文章
iphone http下载文件
查看>>
poj 1195:Mobile phones(二维树状数组,矩阵求和)
查看>>
Codeforces 433 C. Ryouko&#39;s Memory Note
查看>>
java中的Static class
查看>>
实例讲解Linux下的makefile
查看>>
json lib 2.4及其依赖包下载
查看>>
计算机中文核心期刊
查看>>
8148 8168 中移植live55 出现except rtsp 中途莫名的断流
查看>>
【BZOJ】3832: [Poi2014]Rally
查看>>
[转]看懂ExtJS的API
查看>>
推荐15款制作 SVG 动画的 JavaScript 库
查看>>
转:OpenResty最佳实践(推荐了解lua语法)
查看>>
转:CEO, CFO, CIO, CTO, CSO是什么
查看>>
andriod自定义视图
查看>>
linux下vim更改注释颜色
查看>>
在SSL / https下托管SignalR
查看>>
Using JRuby with Maven
查看>>
poj 3308 (最大流)
查看>>
Netty了解与小试
查看>>
醒醒吧少年,只用Cucumber不能帮助你BDD
查看>>