偷偷摘套内射激情视频,久久精品99国产国产精,中文字幕无线乱码人妻,中文在线中文a,性爽19p

Android自定義xml屬性

移動開發(fā) Android
我們平常在xml文件里面定義,設置控件屬性,android:text android:size這類的,有木有可以自定義的呢 答案是肯定的。android這個做的還是比較人性化的,可以自定義屬性,自定義實現(xiàn)風格,主題,之類的。

Android 自定義組件

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

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

布局組件有LinearLayoutFrameLayoutRelativeLayout,absoluteLayout,TabelLayout

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

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

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

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

  1. <?xml version="1.0" encoding="utf-8"?>   
  2. <resources>   
  3.     <declare-styleable name="custom">   
  4.         <attr name="text" format="string" />   
  5.         <attr name="size" format="integer"/>   
  6.         <attr name="color" format="reference|color"/>   
  7.     </declare-styleable>   
  8. </resources>  

 

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

 

創(chuàng)建一個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, 1010, p) ;   
  44.     }   
  45. }   

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

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

5. 運行應用

文章就到此結(jié)束,大家有什么疑問的,請留言,我會及時答復大家!謝謝~

責任編輯:閆佳明 來源: csdn
相關(guān)推薦

2016-12-26 15:25:59

Android自定義View

2022-09-21 14:42:03

JSProps屬性

2016-11-16 21:55:55

源碼分析自定義view androi

2011-08-09 17:16:56

CoreAnimati動畫

2009-08-04 13:35:16

ASP.NET自定義樣

2009-08-06 17:13:56

ASP.NET自定義控

2011-03-17 09:45:01

Spring

2017-05-19 10:03:31

AndroidBaseAdapter實踐

2016-04-12 10:07:55

AndroidViewList

2015-02-12 15:33:43

微信SDK

2010-02-07 14:02:16

Android 界面

2017-05-18 12:36:16

android萬能適配器列表視圖

2010-01-18 15:43:35

VB.NET自定義屬性

2009-06-10 14:02:11

netbeans自定義項目

2021-02-20 11:40:35

SpringBoot占位符開發(fā)技術(shù)

2015-02-12 15:38:26

微信SDK

2013-01-09 17:22:38

Android開發(fā)Camera

2013-05-02 14:08:18

2014-12-10 10:37:45

Android自定義布局

2015-02-11 17:49:35

Android源碼自定義控件
點贊
收藏

51CTO技術(shù)棧公眾號