andriod自訂視圖

來源:互聯網
上載者:User

標籤:style   class   blog   code   http   ext   

一、通過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 還有待考證

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.