Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> Android開發 >> 初級開發 >> Android自定義View以及layout屬性全攻略

Android自定義View以及layout屬性全攻略

編輯:初級開發

對於android系統的自定義View可能大家都熟悉了,對於自定義VIEw的屬性添加,以及Android的Layout的命名空間問題,很多網友還不是很清楚,今天android123一起再帶大家溫習一下

  CwjView myView=new CwjVIEw(context);

  如果用於游戲或整個窗體的界面,我們可能直接在onCreate中setContentView(myVIEw); 當然如果是控件,我們可能會需要從Layout的XML中聲明,比如

  <cn.com.android123.CwjVIEw
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
  />

  當然,我們也可以直接從父類聲明比如

  <VIEw class="cn.com.android123.CwjVIEw"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
  />

上面我們僅用了父類VIEw的兩個屬性,均來自android命名空間,而名稱為layout_width或layout_height,我們自定義的控件可能有更多的功能,比如

    <cn.com.android123.CwjVIEw
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   cwj:age="22"
   cwj:university="sjtu"
   cwj:city="shanghai"
   />

 我們可以看到上面的三個屬性,是我們自定義的。作為標准xml規范,可能還包含了類似 XMLns:android="http://schemas.android.com/apk/res/android"  這樣的語句,對於定義完整的VIEw,我們的命名空間為cwj,這裡可以寫為 XMLns:cwj=http://schemas.android.com/apk/res/cn.com.android123.cwjVIEw 或 XMLns:cwj=http://schemas.android.com/apk/res/android 都可以

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

<?XML version="1.0" encoding="utf-8" ?>
<resources>
  <declare-styleable name="CwjVIEw">
  <attr name="age" format="integer" />
  <attr name="city" format="string" />
  <attr name="university" format="string" />
  </declare-styleable>
</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)的重載類型中可以用

  public CwjVIEw(Context context, AttributeSet attrs) {
  super(context, attrs);
        TypedArray a = context.obtainStyledAttributes(attrs,
          R.styleable.cwj_attr);
        mAge = a.getInteger(R.styleable.CwjVIEw_age, 22);
        mCity = a.getString(R.styleable.CwjVIEw_city, "shanghai");
        mUniversity= a.getString(R.styleable.CwjVIEw_university, "sjtu");
       
        a.recycle(); //android123提示大家不要忘了回收資源

 }

 這樣類的全局成員變量 mAge、mCity就獲取了我們需要的內容,當然根據layout中的數值我們自定義的CwjVIEw需要動態的處理一些數據的情況,可以使用AttributeSet類的getAttributeResourceValue方法獲取。

 public CwjVIEw(Context context, AttributeSet attrs)
 {
  super(context, attrs);
  resId = attrs.getAttributeResourceValue("cn.com.android123.CwjVIEw", "age", 100);  
  resId = attrs.getAttributeResourceValue("cn.com.android123.CwjVIEw", "city", "shanghai");
  //resID就可以任意使用了
 }

以上兩種方法中,參數的最後一個數值為默認的,如果您有不明白的地方可以來函到 [email protected] 我們會在第一時間回復。

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved