Android如何實(shí)現(xiàn)自定義View中attrs.xml-創(chuàng)新互聯(lián)

這篇文章主要為大家展示了“Android如何實(shí)現(xiàn)自定義View中attrs.xml”,內(nèi)容簡而易懂,條理清晰,希望能夠幫助大家解決疑惑,下面讓小編帶領(lǐng)大家一起研究并學(xué)習(xí)一下“Android如何實(shí)現(xiàn)自定義View中attrs.xml”這篇文章吧。

成都服務(wù)器托管,創(chuàng)新互聯(lián)建站提供包括服務(wù)器租用、四川聯(lián)通機(jī)房服務(wù)器托管、帶寬租用、云主機(jī)、機(jī)柜租用、主機(jī)租用托管、CDN網(wǎng)站加速、國際域名空間等業(yè)務(wù)的一體化完整服務(wù)。電話咨詢:13518219792

 Android自定義View中attrs.xml的實(shí)例詳解

我們在自定義View的時(shí)候通常需要先完成attrs.xml文件

在values中定義一個(gè)attrs.xml 然后添加相關(guān)屬性

這一篇先詳細(xì)介紹一下attrs.xml的屬性。

<?xml version="1.0" encoding="utf-8"?>
<resources>
  //自定義屬性名,定義公共屬性
  <attr name="titleText" format="string"/>
  <attr name="titleTextSize" format="dimension"/>
  <attr name="titleTextColor" format="color"/>
  <attr name="image" format="reference"/>
  <attr name="imageScaleType" >
    <enum name="fillXY" value="0"/>
    <enum name="center" value="1"/>
  </attr>

  //自定義控件的主題樣式
  <declare-styleable name="CustomImageView">
    <attr name="titleText" />
    <attr name="titleTextSize" />
    <attr name="titleTextColor" />
    <attr name="image" />
    <attr name="imageScaleType" />
  </declare-styleable>


</resources>

reference:參考某一資源ID。

定義:

<declare-styleable name = "名稱"> 
          <attr name = "background" format = "reference" /> 
</declare-styleable>

使用:

<ImageView 
           android:layout_width = "42dip" 
           android:layout_height = "42dip" 
           android:background = "@drawable/圖片ID" 
           />

color:顏色值

定義:

<declare-styleable name = "名稱"> 
          <attr name = "textColor" format = "color" /> 
      </declare-styleable>

使用:

<TextView 
          android:layout_width = "42dip" 
          android:layout_height = "42dip" 
          android:textColor = "#00FF00" 
          />

boolean:布爾值

定義:

<declare-styleable name = "名稱"> 
        <attr name = "focusable" format = "boolean" /> 
</declare-styleable>

使用:

<Button 
          android:layout_width = "42dip" 
          android:layout_height = "42dip" 
          android:focusable = "true"/>

dimension:尺寸值

定義:

<declare-styleable name = "名稱"> 
          <attr name = "layout_width" format = "dimension" /> 
</declare-styleable>

使用:

<Button 
          android:layout_width = "42dip" 
          android:layout_height = "42dip" 
         />

float:浮點(diǎn)值

定義:

<declare-styleable name = "AlphaAnimation"> 
          <attr name = "fromAlpha" format = "float" /> 
          <attr name = "toAlpha" format = "float" /> 
</declare-styleable>

使用:

<alpha 
    android:fromAlpha = "1.0" 
    android:toAlpha = "0.7" 
/>

integer:整型值

定義:

<declare-styleable name="RotateDrawable"> 
          <attr name = "visible" /> 
          <attr name = "fromDegrees" format = "float" /> 
          <attr name = "toDegrees" format = "float" /> 
          <attr name = "pivotX" format = "fraction" /> 
          <attr name = "pivotY" format = "fraction" /> 
          <attr name = "drawable" /> 
</declare-styleable>

使用:

<rotate 
         xmlns:android = "http://schemas.android.com/apk/res/android"  
         android:interpolator = "@anim/動(dòng)畫ID" 
         android:fromDegrees = "0"  
         android:toDegrees = "360" 
         android:pivotX = "200%" 
         android:pivotY = "300%"  
         android:duration = "5000" 
         android:repeatMode = "restart" 
         android:repeatCount = "infinite" 
        />

enum:枚舉值

定義:

<declare-styleable name="名稱"> 
          <attr name="orientation"> 
             <enum name="horizontal" value="0" /> 
             <enum name="vertical" value="1" /> 
          </attr>       
</declare-styleable>

使用:

<LinearLayout 
          xmlns:android = "http://schemas.android.com/apk/res/android" 
          android:orientation = "vertical" 
          android:layout_width = "fill_parent" 
          android:layout_height = "fill_parent" 
          > 
</LinearLayout>

flag:位或運(yùn)算

<declare-styleable name="名稱"> 
          <attr name="windowSoftInputMode"> 
              <flag name = "stateUnspecified" value = "0" /> 
              <flag name = "stateUnchanged" value = "1" /> 
              <flag name = "stateHidden" value = "2" /> 
              <flag name = "stateAlwaysHidden" value = "3" /> 
              <flag name = "stateVisible" value = "4" /> 
              <flag name = "stateAlwaysVisible" value = "5" /> 
              <flag name = "adjustUnspecified" value = "0x00" /> 
              <flag name = "adjustResize" value = "0x10" /> 
              <flag name = "adjustPan" value = "0x20" /> 
              <flag name = "adjustNothing" value = "0x30" /> 
          </attr>      
lt;/declare-styleable>

使用:

<activity 
   android:name = ".StyleAndThemeActivity" 
   android:label = "@string/app_name" 
   android:windowSoftInputMode = "stateUnspecified | stateUnchanged | stateHidden"> 
   <intent-filter> 
      <action android:name = "android.intent.action.MAIN" /> 
      <category android:name = "android.intent.category.LAUNCHER" /> 
   </intent-filter> 
</activity>

屬性定義時(shí)可以指定多種類型值

定義:

<declare-styleable name = "名稱"> 
   <attr name = "background" format = "reference|color" /> 
</declare-styleable>

使用:

<ImageView 
    android:layout_width = "42dip" 
    android:layout_height = "42dip" 
    android:background = "@drawable/圖片ID|#00FF00" 
    />

以上是“Android如何實(shí)現(xiàn)自定義View中attrs.xml”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對(duì)大家有所幫助,如果還想學(xué)習(xí)更多知識(shí),歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道!

網(wǎng)頁標(biāo)題:Android如何實(shí)現(xiàn)自定義View中attrs.xml-創(chuàng)新互聯(lián)
瀏覽路徑:http://bm7419.com/article16/gicgg.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站內(nèi)鏈網(wǎng)站策劃、移動(dòng)網(wǎng)站建設(shè)網(wǎng)站制作、商城網(wǎng)站、App開發(fā)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

成都做網(wǎng)站