android自定義View的用法

轉自:http://blog.csdn.net/ameyume/article/details/6084062html

1、統一的用戶界面是能夠使得應用程序更友好。要作到用戶界面的統一,咱們就必須用到風格(style)和主題(theme)。
自定義一個View的方法步驟以下:
一、首先,在values文件夾下定義一個atts.xml的文件,描述自定義的控件的屬性
在values/attrs.xml中:
[xhtml] view plain copy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2.  <resources>  
  3.     <declare-styleable name="TestView">  
  4.         <attr name="textColor" format="color" />  
  5.         <attr name="textSize" format="dimension" />  
  6.         <attr name="imgBackground" format="integer" />  
  7.         <attr name="textPaddingLeft" format="dimension"/>  
  8.         <attr name="textPaddingTop" format="dimension"/>  
  9.     </declare-styleable>  
  10.  </resources>  
二、其次,定義一個繼承自View的類,如:TestView,使其實現View的方法
[java] view plain copy
  1. package com.test.TestView;  
  2. import android.content.Context;  
  3. import android.content.res.TypedArray;  
  4. import android.graphics.Canvas;  
  5. import android.graphics.Color;  
  6. import android.graphics.Paint;  
  7. import android.util.AttributeSet;  
  8. import android.view.View;  
  9.   
  10. public class TestView extends View {    
  11.     private Paint mPaint;  
  12.     private Context mContext;  
  13.     private String mStr;  
  14.     public TestView(Context context, AttributeSet attrs) {//構造方法;根據須要實現繼承自View的方法  
  15.         super(context, attrs);  
  16.         mContext = context;  
  17.         initMyView();  
  18.         //對於咱們自定義的類中,咱們須要使用一個名爲obtainStyledAttributes的方法來獲取咱們的定義。  
  19.         TypedArray params = context.obtainStyledAttributes(attrs,R.styleable.MyView);  
  20.         //獲得自定義控件的屬性值。  
  21.         int backgroudId = params.getResourceId(R.styleable.MyView_imgBackground, 0);  
  22.         if (backgroudId != 0)  
  23.             setBackgroundResource(backgroudId);  
  24.         int textColor = params.getColor(R.styleable.MyView_textColor,0XFFFFFFFF);  
  25.         setTextColor(textColor);  
  26.         float textSize = params.getDimension(R.styleable.MyView_textSize, 36);  
  27.         setTextSize(textSize);  
  28.         float paddingLeft = params.getDimension( R.styleable.MyView_textPaddingLeft, 41);  
  29.         float paddingTop = params.getDimension(R.styleable.MyView_textPaddingTop, 21);  
  30.         setPaddings(paddingLeft, paddingTop);  
  31.     }  
  32.     @Override  
  33.     protected void onDraw(Canvas canvas) {  
  34.         super.onDraw(canvas);  
  35.         if (mStr != null) {  
  36.             canvas.drawText(mStr, 3060, mPaint);  
  37.         }  
  38.     }  
  39.     private void initMyView() {  
  40.         mPaint = new Paint();  
  41.         mPaint.setColor(Color.WHITE);  
  42.     }  
  43.     private void setTextColor(int textColor) {  
  44.         mPaint.setColor(0XFFAABBCC);  
  45.     }  
  46.     private void setTextSize(float textSize) {  
  47.         mPaint.setTextSize(textSize);  
  48.     }  
  49.     void setText(String text) {  
  50.         mStr = text;  
  51.     }  
  52.     private void setPaddings(float paddingLeft, float paddingTop) {  
  53.         setPadding((int) paddingLeft, (int) paddingTop, 00);  
  54.     }  
  55. }  
三、而後,在Layout文件中應用該自定義的view,以下:
如在main.xml中
[xhtml] view plain copy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2.  <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.                xmlns:app="http://schemas.android.com/apk/res/com.test.TestView"    
  4.                android:orientation="vertical" android:layout_width="fill_parent"  
  5.                android:layout_height="fill_parent">  
  6.     <com.test.TestView.TestView  
  7.               android:id="@+id/myview"  
  8.               android:layout_width="fill_parent"  
  9.               android:layout_height="fill_parent"  
  10.               app:textColor="#FFFFFFFF"  
  11.               app:textSize="40dip"  
  12.               app:textPaddingLeft="40dip"  
  13.               app:textPaddingTop="40dip"  
  14.               app:imgBackground="@drawable/bg" />  
  15.  </LinearLayout>  
說明: 上述紅色部分——xmlns:app="http://schemas.android.com/apk/res/com.test.TestView"是首先聲明命名空間。命名空間爲app.路徑是 http://schemas.android.com/apk/res/ 這一部分是不變的,後面接的是R的路徑:com.test.TestView.R。而後在自定義控件的xml描述中就能夠這樣使用app:value="true"。這樣就實現了自定義控件的初始化賦值。
四、而後就是使用了,在本身的Activity 中
[java] view plain copy
  1. public class TestActivity extends Activity {  
  2.      
  3.     @Override  
  4.     public void onCreate(Bundle savedInstanceState) {  
  5.         super.onCreate(savedInstanceState);  
  6.         setContentView(R.layout.main);  
  7.          
  8.         TestView mTextView=(TestView)findViewById(R.id.TestView);  
  9.         mTextView.setText("自定義的View");  
  10.     }  
  11. }  

五、另外:java

如下內容轉自:http://www.android123.com.cn/androidkaifa/591.htmlandroid

    對於Android系統的自定義View可能你們都熟悉了,對於自定義View的屬性添加,以及Android的Layout的命名空間問題,不少網友還不是很清楚,今天Android123一塊兒再帶你們溫習一下canvas

[java] view plain copy
  1. CwjView myView=new CwjView(context);  
      

  若是用於遊戲或整個窗體的界面,咱們可能直接在onCreate中setContentView(myView); 固然若是是控件,咱們可能會須要從Layout的xml中聲明,好比app

[xhtml] view plain copy
  1. <cn.com.android123.CwjView  
  2.        android:layout_width="wrap_content"  
  3.        android:layout_height="wrap_content"  
  4. />  

  固然,咱們也能夠直接從父類聲明好比ide

[xhtml] view plain copy
  1. <View class="cn.com.android123.CwjView"  
  2.       android:layout_width="wrap_content"  
  3.       android:layout_height="wrap_content"  
  4. />  

   上面咱們僅用了父類View的兩個屬性,均來自android命名空間,而名稱爲layout_width或layout_height,咱們自定義的控件可能有更多的功能,好比編碼

[xhtml] view plain copy
  1. <cn.com.android123.CwjView   
  2.       android:layout_width="wrap_content"  
  3.       android:layout_height="wrap_content"  
  4.      cwj:age="22"  
  5.      cwj:university="sjtu"  
  6.      cwj:city="shanghai"  
  7. />  

 咱們能夠看到上面的三個屬性,是咱們自定義的。做爲標準xml規範,可能還包含了相似 xmlns:android="http://schemas.android.com/apk/res/android"  這樣的語句,對於定義完整的View,咱們的命名空間爲cwj,這裏能夠寫爲  spa

       xmlns:cwj=http://schemas.android.com/apk/res/cn.com.android123.cwjView 或        .net

       xmlns:cwj=http://schemas.android.com/apk/res/android 均可以3d

  對於定義的cwj命名空間和age、university以及city的三個屬性咱們如何定義呢? 在工程的res/values目錄中咱們新建一個cwj_attr.xml文件,編碼方式爲utf-8是一個好習慣,內容以下

[xhtml] view plain copy
  1. <?xml version="1.0" encoding="utf-8" ?>  
  2. <resources>  
  3.     <declare-styleable name="CwjView">  
  4.        <attr name="age" format="integer" />  
  5.        <attr name="city" format="string" />  
  6.        <attr name="university" format="string" />  
  7.    </declare-styleable>  
  8. </resources>  

  這裏咱們可能對format不是很熟悉,目前Android系統內置的格式類型有integer好比 ProgressBar的進度值,float好比RatingBar的值多是3.5顆星,boolean好比ToggleButton的是否勾 選,string好比TextView的text屬性,固然除了咱們常見的基礎類型外,Android的屬性還有特殊的好比color是用於顏色屬性的, 能夠識別爲#FF0000等類型,固然還有dimension的尺寸類型,好比23dip,15px,18sp的長度單位,還有一種特殊的爲 reference,通常用於引用@+id/cwj @drawable/xxx這樣的類型。

  固然何時用reference呢? 咱們就以定義一個顏色爲例子:

  <attr name="red" format="color|reference" />  這裏咱們用了邏輯或的運算符,定義的紅色是顏色類型的,同時能夠被引用。固然,對於咱們自定義的類中,咱們須要使用一個名爲 obtainStyledAttributes的方法來獲取咱們的定義。在咱們自定義View的構造方法(Context context, AttributeSet attrs)的重載類型中能夠用

[java] view plain copy
  1. public CwjView(Context context, AttributeSet attrs) {  
  2.     super(context, attrs);  
  3.     TypedArray a = context.obtainStyledAttributes(attrs,R.styleable.cwj_attr);  
  4.     mAge = a.getInteger(R.styleable.CwjView_age, 22);  
  5.     mCity = a.getString(R.styleable.CwjView_city, "shanghai");  
  6.     mUniversity= a.getString(R.styleable.CwjView_university, "sjtu");          
  7.     a.recycle(); //Android123提示你們不要忘了回收資源  
  8.   
  9. }  

這樣類的全局成員變量 mAge、mCity就獲取了咱們須要的內容,固然根據layout中的數值咱們自定義的CwjView須要動態的處理一些數據的狀況,能夠使用AttributeSet類的getAttributeResourceValue方法獲取。

[java] view plain copy
  1. public CwjView(Context context, AttributeSet attrs){  
  2.     super(context, attrs);  
  3.     resId = attrs.getAttributeResourceValue("cn.com.android123.CwjView""age"100);    
  4.     resId = attrs.getAttributeResourceValue("cn.com.android123.CwjView""city""shanghai");  
  5.     //resID就能夠任意使用了  

以上兩種方法中,參數的最後一個數值爲默認的,若是您有不明白的地方能夠來函到 android123@163.com 咱們會在第一時間回覆。

相關文章
相關標籤/搜索