zoukankan      html  css  js  c++  java
  • andriod自定义视图

    一、通过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;
    }

    处理用户交互事件

    image

    使用自定义控件

    <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 还有待考证

  • 相关阅读:
    线程的创建和运行
    Spring SpringMvc Mybatis Maven整合
    使用 Redis 缓存来实现用户最近浏览的商品列表
    Java泛型
    HDFS的HA集群原理分析
    MapReduce-自动化运行配置
    大数据-HDFS 集群搭建的配置文件
    鼠标线状移动特效
    Mac 终端 Tomcat 环境配置过程
    oracle列转行
  • 原文地址:https://www.cnblogs.com/lzhp/p/3807250.html
Copyright © 2011-2022 走看看