1. 程式人生 > >Android中attrs.xml檔案的使用詳解

Android中attrs.xml檔案的使用詳解

1. attrs.xml 的作用

控制元件有很多屬性,如 android:id、android:layout_width、android:layout_height等,但是這些屬性都是系統自帶的屬性。使用attrs.xml檔案,可以自己定義屬性,下面我會寫些小 demo ,比較好理解

2. 在values資料夾下,新建一個attrs.xml檔案

內容如下:


<?xml version="1.0" encoding="utf-8"?>
<declare-styleable name="MyView">
        <attr name="textColor"
format="color" />
<attr name="textSize" format="dimension" /> </declare-styleable>

其中,

<declare-styleable name="MyView">

表明樣式名稱為MyView,下面包含了兩個自定義屬性textColor和textSize,其中textColor是顏色(color)類的屬性,textSize是尺寸(dimension)類的屬性

3. 自定義 MyView

public class MyView extends View
{
private Paint mPaint; private static final String mString = "Welcome to BaiYe's blog"; public MyView(Context context) { this(context,null); } public MyView(Context context, AttributeSet attrs) { super(context, attrs); mPaint = new Paint(); TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.MyView); int
textColor = a.getColor(R.styleable.MyView_textColor, 0XFFFFFFFF); float textSize = a.getDimension(R.styleable.MyView_textSize, 36); mPaint.setTextSize(textSize); mPaint.setColor(textColor); a.recycle(); } @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); // 設定填充 mPaint.setStyle(Paint.Style.FILL); // 畫一個矩形,前倆個是矩形左上角座標,後面倆個是右下角座標 // mPaint.setColor(Color.BLACK); canvas.drawRect(new Rect(10, 10, 100, 100), mPaint); // 繪製文字 canvas.drawText(mString, 60, 410, mPaint); } }
  • 首先從 R.styleable.CustomView 獲得了TypedArray變數
  • 再用getColor(),getDimension()等方法獲取相應的屬性值,屬性格式為“樣式名_屬性名”,屬性後面的引數是預設值。
  • 獲得屬性值以後,就可以應用這些屬性值。
  • recycle()方法用於返回訊號給資源(不懂什麼意思)

4. xml 內容

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:test="http://schemas.android.com/apk/res-auto"//一定記得新增字首 
    android:id="@+id/activity_attrs_actiity"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.lizi.newset.CustomView.attrs.AttrsActivity">

    <com.lizi.newset.CustomView.attrs.MyView
        android:id="@+id/myView"
        android:layout_height="match_parent"
        android:layout_width="wrap_content"
        test:textSize="50px"
        test:textColor="#ff00ff"/>
    />
</RelativeLayout>

xmlns:test=”http://schemas.android.com/apk/res-auto”一定要新增,新增之後才能在xml中自定義屬性

5. 自定義屬性

格式如上,其中“xmlns:test”冒號後面是標籤名,在下面使用時(只對當前檔案可用)

<TextView  test:屬性名/>

5.1 reference:參考某一資源ID


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

eg:

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

5.2 color:顏色值


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

eg:


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

5.3 boolean:布林值


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

eg:


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

5.4 dimension:尺寸值


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

eg:

<com.lizi.newset.CustomView.attrs.MyView
        android:id="@+id/myView"
        android:layout_height="match_parent"
        android:layout_width="wrap_content"
        test:textSize="50px"
        test:textColor="#ff00ff"/>

5.5 float:浮點值


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

eg:


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

5.6 string:字串


<declare-styleable name = "MapView">
     <attr name = "apiKey" format = "string" />
</declare-styleable>

eg:


<com.google.android.maps.MapView
         android:layout_width = "fill_parent"
         android:layout_height = "fill_parent"
         android:apiKey = "0jOkQ80oD1JL9C6HAja99uGXCRiS2CGjKO_bc_g"
                    />

5.7 integer:整型值 || fraction:百分數


<declare-styleable name = "AnimatedRotateDrawable">
      <attr name = "visible" />
      <attr name = "frameDuration" format="integer" />
      <attr name = "framesCount" format="integer" />
      <attr name = "pivotX"  format = "fraction"/>
      <attr name = "pivotY"  format = "fraction"/>
      <attr name = "drawable" />
</declare-styleable>

eg:


<animated-rotate
      xmlns:android = "http://schemas.android.com/apk/res/android"  
      android:drawable = "@drawable/圖片ID"  
      android:pivotX = "50%"  
      android:pivotY = "50%"  
      android:framesCount = "12"  
      android:frameDuration = "100"
  />

5.8 enum:列舉值


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

eg:


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

5.9 flag 位或運算


 <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>         
</declare-styleable>

eg:


<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>

6. 屬性定義時可以同時定義多種型別值


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

eg:


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

“`