• Android高手进阶教程(三)之Android 中自定义View的应用.


    大家好我们今天的教程是在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>   
    最后执行之,效果如下图:
     
    OK,大功告成,今天就写到这里,开始做饭了,老婆孩子等我做饭了,lol~
     
     

    本文出自 “Android_Tutor” 博客,请务必保留此出处http://weizhulin.blog.51cto.com/1556324/311457

  • 相关阅读:
    SVN 客户端的使用
    day36_Spring学习笔记_04_SVN
    VisualSVN Server 的使用图解(windows版本)
    day68_淘淘商城项目_01_电商介绍 + 互联网术语 + SOA + 分布式 + 集群介绍 + 环境配置 + 框架搭建_匠心笔记
    VisualSVN Server 的安装(windows版本)
    【代码规范神器】阿里巴巴Java开发规约IDE插件使用教程(P3C)
    学了这四招,你在Linux上观看Netflix视频不发愁
    如何在Fedora或CentOS上使用Samba共享
    Fedora 23如何安装LAMP服务器
    HTTP/HTTPS自动加密上网方案
  • 原文地址:https://www.cnblogs.com/xiaoran1129/p/2814032.html
Copyright © 2020-2023  润新知