成人免费xxxxx在线视频软件_久久精品久久久_亚洲国产精品久久久_天天色天天色_亚洲人成一区_欧美一级欧美三级在线观看

Android自定義xml屬性

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

Android 自定義組件

Android 提供了非常精致的和非常強大的組件化模型,能夠更加方便的構建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進行小部分的調整,那就可以通過重寫部分一些方法來完成開發(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. 運行應用

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

責任編輯:閆佳明 來源: csdn
相關推薦

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ā)技術

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技術棧公眾號

主站蜘蛛池模板: 999久久久免费精品国产 | 99草免费视频 | 精品久久亚洲 | 亚洲国产精品成人综合久久久 | 99亚洲 | 日日骚网 | 亚洲网站在线播放 | 天天天天天天操 | 蜜桃黄网| 激情久久网| 久久69精品久久久久久久电影好 | 欧美午夜激情在线 | 国产视频黄色 | 久久99国产精品 | 国产在线播放av | 日韩精品久久一区二区三区 | h视频免费在线观看 | 久久久精品久久久 | 国产99久久久国产精品 | 日日操日日干 | 国产91久久久久蜜臀青青天草二 | 欧美一区不卡 | 国产精品高清一区二区 | 黄色免费网 | 日韩在线一区二区三区 | 久久99精品久久久久 | 久久福利电影 | 欧美精品久久 | 亚洲精品www | 国产在线小视频 | 中文字幕99| 欧美日韩一区二区三区四区 | 婷婷丁香综合网 | 久久久久久久久久久成人 | 国产精品高潮呻吟久久久久 | 日本一二区视频 | 精品视频免费 | 日本黄视频在线观看 | 精品一区二区久久久久久久网站 | 欧美 日韩 国产 成人 在线 91 | 羞羞的视频网站 |