android 自定義view組件

 

原創作品,允許轉載,轉載時請務必以超鏈接形式標明文章 原始出處 、作者信息和本聲明。否則將追究法律責任。http://weizhulin.blog.51cto.com/1556324/311457

大家好我們今天的教程是在Android 教程中自定義View 的學習,對於初學着來說,他們習慣了Android 傳統的頁面佈局方式,如下代碼:
view plaincopy to clipboardprint?
 
  1. <?xml version="1.0" encoding="utf-8"?>     
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    
  3.     android:orientation="vertical"    
  4.     android:layout_width="fill_parent"    
  5.     android:layout_height="fill_parent"    
  6.     >     
  7. <TextView       
  8.     android:layout_width="fill_parent"      
  9.     android:layout_height="wrap_content"      
  10.     android:text="@string/hello"    
  11.     />     
  12. </LinearLayout>    
  13. <?xml version="1.0" encoding="utf-8"?> 
  14. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
  15.     android:orientation="vertical" 
  16.     android:layout_width="fill_parent" 
  17.     android:layout_height="fill_parent" 
  18.     > 
  19. <TextView    
  20.     android:layout_width="fill_parent"   
  21.     android:layout_height="wrap_content"   
  22.     android:text="@string/hello" 
  23.     /> 
  24. </LinearLayout>   
當然上面的佈局方式可以幫助我們完成簡單應用的開發了,但是如果你想寫一個複雜的應用,這樣就有點牽強了,大家不信可以下源碼都研究看看,高手寫的佈局方式,如上面的佈局高手通常是這樣寫的:
view plaincopy to clipboardprint?
 
  1. <?xml version="1.0" encoding="utf-8"?>     
  2. <A>     
  3.     <B></B>     
  4. </A>    
  5. <?xml version="1.0" encoding="utf-8"?> 
  6. <A> 
  7.  <B></B> 
  8. </A>   
view plaincopy to clipboardprint?
其中A extends LinerLayout, B extends TextView. 
其中A extends LinerLayout, B extends TextView.
爲了幫助大家更容易理解,我寫了一個簡單的Demo ,具體步驟如下:
首先新建一個Android 工程 命名爲ViewDemo .
然後自定義一個View 類,命名爲MyView(extends View) .代碼如下:
  1. view plaincopy to clipboardprint?  
  2. package com.android.tutor;     
  3. import android.content.Context;     
  4. import android.graphics.Canvas;     
  5. import android.graphics.Color;     
  6. import android.graphics.Paint;     
  7. import android.graphics.Rect;     
  8. import android.graphics.Paint.Style;     
  9. import android.util.AttributeSet;     
  10. import android.view.View;     
  11. public class MyView extends View {     
  12.     private Paint mPaint;     
  13.     private Context mContext;     
  14.     private static final String mString = "Welcome to Mr Wei's blog";     
  15.          
  16.     public MyView(Context context) {     
  17.         super(context);     
  18.          
  19.     }     
  20.     public MyView(Context context,AttributeSet attr)     
  21.     {     
  22.         super(context,attr);     
  23.          
  24.     }     
  25.     @Override    
  26.     protected void onDraw(Canvas canvas) {     
  27.         // TODO Auto-generated method stub     
  28.         super.onDraw(canvas);     
  29.              
  30.         mPaint = new Paint();     
  31.              
  32.         //設置畫筆顏色     
  33.         mPaint.setColor(Color.RED);     
  34.         //設置填充     
  35.         mPaint.setStyle(Style.FILL);     
  36.              
  37.         //畫一個矩形,前倆個是矩形左上角座標,後面倆個是右下角座標     
  38.         canvas.drawRect(new Rect(10, 10, 100, 100), mPaint);     
  39.              
  40.         mPaint.setColor(Color.BLUE);     
  41.         //繪製文字     
  42.         canvas.drawText(mString, 10, 110, mPaint);     
  43.     }     
  44. }    
  45. package com.android.tutor;  
  46. import android.content.Context;  
  47. import android.graphics.Canvas;  
  48. import android.graphics.Color;  
  49. import android.graphics.Paint;  
  50. import android.graphics.Rect;  
  51. import android.graphics.Paint.Style;  
  52. import android.util.AttributeSet;  
  53. import android.view.View;  
  54. public class MyView extends View {  
  55.  private Paint mPaint;  
  56.  private Context mContext;  
  57.  private static final String mString = "Welcome to Mr Wei's blog";  
  58.    
  59.  public MyView(Context context) {  
  60.   super(context);  
  61.    
  62.  }  
  63.  public MyView(Context context,AttributeSet attr)  
  64.  {  
  65.   super(context,attr);  
  66.    
  67.  }  
  68.  @Override  
  69.  protected void onDraw(Canvas canvas) {  
  70.   // TODO Auto-generated method stub  
  71.   super.onDraw(canvas);  
  72.     
  73.   mPaint = new Paint();  
  74.     
  75.   //設置畫筆顏色  
  76.   mPaint.setColor(Color.RED);  
  77.   //設置填充  
  78.   mPaint.setStyle(Style.FILL);  
  79.     
  80.   //畫一個矩形,前倆個是矩形左上角座標,後面倆個是右下角座標  
  81.   canvas.drawRect(new Rect(10, 10, 100, 100), mPaint);  
  82.     
  83.   mPaint.setColor(Color.BLUE);  
  84.   //繪製文字  
  85.   canvas.drawText(mString, 10, 110, mPaint);  
  86.  }  
  87. }  
  88.    
  89. 然後將我們自定義的View 加入到main.xml 佈局文件中,代碼如下:  
  90. view plaincopy to clipboardprint?  
  91. <?xml version="1.0" encoding="utf-8"?>     
  92. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    
  93.     android:orientation="vertical"    
  94.     android:layout_width="fill_parent"    
  95.     android:layout_height="fill_parent"    
  96.     >     
  97. <TextView       
  98.     android:layout_width="fill_parent"      
  99.     android:layout_height="wrap_content"      
  100.     android:text="@string/hello"    
  101.     />     
  102. <com.android.tutor.MyView     
  103.     android:layout_width="fill_parent"      
  104.     android:layout_height="fill_parent"      
  105. />     
  106. </LinearLayout>    
  107. <?xml version="1.0" encoding="utf-8"?> 
  108. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
  109.     android:orientation="vertical" 
  110.     android:layout_width="fill_parent" 
  111.     android:layout_height="fill_parent" 
  112.     > 
  113. <TextView    
  114.     android:layout_width="fill_parent"   
  115.     android:layout_height="wrap_content"   
  116.     android:text="@string/hello" 
  117.     /> 
  118. <com.android.tutor.MyView 
  119.  android:layout_width="fill_parent"   
  120.     android:layout_height="fill_parent"   
  121. /> 
  122. </LinearLayout> 

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章