• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            大龍的博客

            常用鏈接

            統(tǒng)計

            最新評論

            Android 中自定義View的應(yīng)用 (自繪TextView) ---- 轉(zhuǎn)

            大家好我們今天的教程是在Android 教程中自定義View 的學(xué)習(xí),對于初學(xué)著來說,他們習(xí)慣了Android 傳統(tǒng)的頁面布局方式,如下代碼:

            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>  

            當(dāng)然上面的布局方式可以幫助我們完成簡單應(yīng)用的開發(fā)了,但是如果你想寫一個復(fù)雜的應(yīng)用,這樣就有點牽強(qiáng)了,大家不信可以下源碼都研究看看,高手寫的布局方式,如上面的布局高手通常是這樣寫的:

            1. <?xml version="1.0" encoding="utf-8"?>  
            2. <A>  
            3.     <B></B>  
            4. </A>  

            1. 其中A extends LinerLayout, B extends TextView.  

            為了幫助大家更容易理解,我寫了一個簡單的Demo ,具體步驟如下:

            首先新建一個Android 工程 命名為ViewDemo .

            然后自定義一個View 類,命名為MyView(extends View) .代碼如下:

            1. package com.android.tutor;  
            2. import android.content.Context;  
            3. import android.graphics.Canvas;  
            4. import android.graphics.Color;  
            5. import android.graphics.Paint;  
            6. import android.graphics.Rect;  
            7. import android.graphics.Paint.Style;  
            8. import android.util.AttributeSet;  
            9. import android.view.View;  
            10. public class MyView extends View {  
            11.     private Paint mPaint;  
            12.     private Context mContext;  
            13.     private static final String mString = "Welcome to Mr Wei's blog";  
            14.       
            15.     public MyView(Context context) {  
            16.         super(context);  
            17.       
            18.     }  
            19.     public MyView(Context context,AttributeSet attr)  
            20.     {  
            21.         super(context,attr);  
            22.       
            23.     }  
            24.     @Override  
            25.     protected void onDraw(Canvas canvas) {  
            26.         // TODO Auto-generated method stub  
            27.         super.onDraw(canvas);  
            28.           
            29.         mPaint = new Paint();  
            30.           
            31.         //設(shè)置畫筆顏色  
            32.         mPaint.setColor(Color.RED);  
            33.         //設(shè)置填充  
            34.         mPaint.setStyle(Style.FILL);  
            35.           
            36.         //畫一個矩形,前倆個是矩形左上角坐標(biāo),后面?zhèn)z個是右下角坐標(biāo)  
            37.         canvas.drawRect(new Rect(1010100100), mPaint);  
            38.           
            39.         mPaint.setColor(Color.BLUE);  
            40.         //繪制文字  
            41.         canvas.drawText(mString, 10110, mPaint);  
            42.     }  
            43. }  

            然后將我們自定義的View 加入到main.xml 布局文件中,代碼如下:

            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. <com.android.tutor.MyView  
            13.     android:layout_width="fill_parent"   
            14.     android:layout_height="fill_parent"   
            15. />  
            16. </LinearLayout>  

            最后執(zhí)行之,效果如下圖:

            OK,大功告成,今天就寫到這里,開始做飯了,老婆孩子等我做飯了,lol~

            posted on 2010-06-17 21:30 大龍 閱讀(3311) 評論(0)  編輯 收藏 引用


            只有注冊用戶登錄后才能發(fā)表評論。
            網(wǎng)站導(dǎo)航: 博客園   IT新聞   BlogJava   博問   Chat2DB   管理


            色偷偷88欧美精品久久久| 久久久久女人精品毛片| 亚洲AV日韩AV永久无码久久| 国产精品久久久久AV福利动漫| 久久精品无码一区二区日韩AV| 人妻精品久久久久中文字幕69| 久久人妻少妇嫩草AV蜜桃| 狠狠色噜噜狠狠狠狠狠色综合久久 | 久久综合九色综合久99| 久久精品中文字幕无码绿巨人| 亚洲国产精品无码久久九九| 久久精品国产亚洲网站| 人妻精品久久久久中文字幕一冢本| 久久久久无码国产精品不卡| 久久国产精品久久精品国产| 色欲综合久久躁天天躁蜜桃| 亚洲欧美久久久久9999 | 久久天天躁狠狠躁夜夜网站| 亚洲国产综合久久天堂 | 无码八A片人妻少妇久久| 久久久久国色AV免费观看| 97超级碰碰碰碰久久久久| 久久久精品人妻一区二区三区蜜桃| 一本久久综合亚洲鲁鲁五月天亚洲欧美一区二区 | 国产产无码乱码精品久久鸭| 亚洲乱码精品久久久久.. | 久久精品国产亚洲AV忘忧草18 | 久久伊人色| 久久99热这里只有精品国产| 国产69精品久久久久9999| 国产精品久久久久久久久免费| 久久福利青草精品资源站免费| 久久久亚洲欧洲日产国码二区| 久久久这里有精品中文字幕| 午夜精品久久久久久| yy6080久久| 久久久久亚洲av成人网人人软件 | 久久精品视频一| 思思久久99热只有频精品66| 久久亚洲精品国产精品婷婷| 久久婷婷人人澡人人爽人人爱 |