日本综合一区二区|亚洲中文天堂综合|日韩欧美自拍一区|男女精品天堂一区|欧美自拍第6页亚洲成人精品一区|亚洲黄色天堂一区二区成人|超碰91偷拍第一页|日韩av夜夜嗨中文字幕|久久蜜综合视频官网|精美人妻一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時(shí)間:8:30-17:00
你可能遇到了下面的問題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
Android自定義xml屬性

Android 自定義組件

創(chuàng)新互聯(lián)是一家專業(yè)的成都網(wǎng)站建設(shè)公司,我們專注網(wǎng)站設(shè)計(jì)、成都網(wǎng)站制作、網(wǎng)絡(luò)營銷、企業(yè)網(wǎng)站建設(shè),買鏈接,廣告投放平臺(tái)為企業(yè)客戶提供一站式建站解決方案,能帶給客戶新的互聯(lián)網(wǎng)理念。從網(wǎng)站結(jié)構(gòu)的規(guī)劃UI設(shè)計(jì)到用戶體驗(yàn)提高,創(chuàng)新互聯(lián)力求做到盡善盡美。

Android 提供了非常精致的和非常強(qiáng)大的組件化模型,能夠更加方便的構(gòu)建UI,這些UI組件都是基于基本的layout類:View 和 ViewGroup。

部分能夠用的widgets包括:Button,TextView,EditText,ListView,CheckBox,RadioButton,Gallery,Spinner,和一些比較特殊用途的widgets(AutoCompleteTextView, ImageSwitcher, and TextSwitcher.)

布局組件有LinearLayout, FrameLayout, RelativeLayout,absoluteLayout,TabelLayout

如果預(yù)定義的widgets和布局組件都不符合您的需求,那就需要?jiǎng)?chuàng)建屬于自己的view,如果只是需要對(duì)已有的widget和layout進(jìn)行小部分的調(diào)整,那就可以通過重寫部分一些方法來完成開發(fā)。

下面就舉個(gè)例子講解如何創(chuàng)建自定義的xml屬性,以及如果使用。

1. 首先創(chuàng)建一個(gè)新的android application.

2. 創(chuàng)建屬性
在res/values/ 下創(chuàng)建一個(gè)attr.xml 文件,定義好需要的attributes

 
 
 
 
  1.    
  2.    
  3.        
  4.            
  5.            
  6.            
  7.        
  8.   

 

3. 創(chuàng)建自定義的View

創(chuàng)建一個(gè)View, CustomView 繼承自View(根據(jù)具體的情況,如果需求和已經(jīng)存在的widget或者layout相差不大,就繼承,重寫一些方法)

 
 
 
 
  1. package com.hualu.androidview;   
  2. import android.content.Context;   
  3. import android.content.res.TypedArray;   
  4. import android.graphics.Canvas;   
  5. import android.graphics.Paint;   
  6. import android.util.AttributeSet;   
  7. import android.view.View;   
  8. public class CustomView extends View {   
  9.     private  Paint p = null;   
  10.     private String text =  null;   
  11.     public CustomView(Context context) {   
  12.         super(context);   
  13.         initCustomView() ;   
  14.     }   
  15.     public CustomView(Context context, AttributeSet attrs){   
  16.         super(context, attrs ) ;   
  17.         initCustomView() ;   
  18.         TypedArray a = context.obtainStyledAttributes(attrs,R.styleable.custom) ;   
  19.         int indexCount = a.getIndexCount() ;   
  20.         for(int i = 0 ; i < indexCount ; i ++){   
  21.             int index = a.getIndex(i) ;   
  22.             switch (index) {   
  23.             case R.styleable.custom_text:   
  24.                 text = a.getString(index) ;   
  25.                 break;   
  26.             case R.styleable.custom_size:   
  27.                 p.setTextSize(a.getInt(index, 0));    
  28.                 break;   
  29.             case R.styleable.custom_color:   
  30.                 p.setColor(a.getColor(index, 0xFF000000)) ;   
  31.                 break;   
  32.             }   
  33.         }   
  34.         a.recycle() ;   
  35.     }  
  36.     void initCustomView(){   
  37.          p = new Paint();   
  38.          p.setAntiAlias(true);   
  39.     } ;   
  40.     @Override   
  41.     protected void onDraw(Canvas canvas) {   
  42.         super.onDraw(canvas);   
  43.         canvas.drawText(text, 10, 10, p) ;   
  44.     }   
  45. }   

4. 在layout的文件使用自定義的view

 
 
 
 
  1.     xmlns:custom="http://schemas.android.com/apk/res/com.hualu.androidview"   
  2.     xmlns:tools="http://schemas.android.com/tools"   
  3.     android:layout_width="match_parent"   
  4.     android:layout_height="match_parent"   
  5.     tools:context=".MainActivity" >   
  6.     
  7.         android:layout_width="wrap_content"   
  8.         android:layout_height="wrap_content"   
  9.         android:layout_centerHorizontal="true"   
  10.         android:layout_centerVertical="true"   
  11.         android:text="@string/hello_world" />   
  12.     
  13.         android:layout_width="wrap_content"   
  14.         android:layout_height="wrap_content"   
  15.         custom:text="custom view"   
  16.         custom:color="#00FF00"   
  17.         custom:size="18"   
  18.         />   
  19.  

5. 運(yùn)行應(yīng)用

文章就到此結(jié)束,大家有什么疑問的,請(qǐng)留言,我會(huì)及時(shí)答復(fù)大家!謝謝~


分享文章:Android自定義xml屬性
瀏覽地址:http://www.dlmjj.cn/article/cccjsed.html