Android自定義控件使用attrs屬性

最近一直在忙項目,只好週末整理一下代碼。自己做筆記的同時與大家一起學習交流。

一、attrs.xml文件的中屬性類型format值的格式

"reference" //引用
"color" //顏色
"boolean" //布爾值
"dimension" //尺寸值
"float" //浮點值
"integer" //整型值
"string" //字符串
"fraction" //百分數,比如200%

枚舉型的格式:

< attr name="orientation">
  < enum name="horizontal" value="0" />
  < enum name="vertical" value="1" />
< /attr>

XML文件中使用:

android:orientation = "vertical"

標誌位、位或運算,格式如下:

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

XML文件中使用:

android:windowSoftInputMode = "stateUnspecified | stateUnchanged | stateHidden">

屬性定義可以指定多種類型:

< attr name = "background" format = "reference|color" />

XML文件中使用:

android:background = "@drawable/圖片ID|#00FF00"

以上內容轉自http://blog.csdn.net/wxg630815/article/details/6989316

二、自定義控件的使用

1、自定義控件在佈局中使用,只需要寫入類名就可以了。其他屬性和一般控件寫法一樣。如:android:layout_width=”wrap_content”等。

<com.chengkni.demo.attrs.MyView      xmlns:test="http://schemas.android.com/apk/res/com.chengkni.demo.attrs"
  android:id="@+id/view_main_my_view"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  test:tText="hello word" />

2、如果自定義的控件想使用自己定義的屬性標籤。在佈局中添加命名空間“xmlns:‘名字’=”http://schemas.android.com/apk/res/‘自己軟件包名’“。然後在工程中values文件下添加attrs.xml文件。

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <declare-styleable name="CustomView">
        <attr name="tText" format="string" />
    </declare-styleable>
</resources>

添加完成後只需在佈局中寫出標籤的內容即可。如上邊佈局文件,xmlns:test=”http://schemas.android.com/apk/res/com.chengkni.demo.attrs”就可以使用test這個命名空間,然後通過命名空間使用attrs.xml下的tText標籤。

3、我們定義的自定義的控件MyView,繼承了View。控件有個字段mViewName用來標記改控件的名字。我們可以通過在佈局文件設置自定義屬性就可以在實現給MyView的mViewName命名。

package com.chengkni.demo.attrs;

import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;

public class MyView extends View {
    private String TAG = this.getClass().getSimpleName();
    private String mViewName;

    public MyView(Context context) {
        super(context);
        Log.e(TAG, "---------- MyView(1) ----------");
        init(context, null);
    }

    public MyView(Context context, AttributeSet attrs) {
        super(context, attrs);
        Log.e(TAG, "---------- MyView(2) ----------");
        init(context, attrs);
    }

    public MyView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        Log.e(TAG, "---------- MyView(3) ----------");
        init(context, attrs);
    }

    private void init(Context context, AttributeSet attrs) {
        if (attrs == null) {
            mViewName = "Attrs is null";
        } else {
            TypedArray array = context.obtainStyledAttributes(attrs,
                    R.styleable.CustomView);
            mViewName = array.getString(R.styleable.CustomView_tText);
            array.recycle();
        }
        Log.e(TAG, "mString = " + mViewName);
    }

    public String getmViewName() {
        return mViewName;
    }

    public void setmViewName(String mViewName) {
        this.mViewName = mViewName;
    }
}

完整工程“Android自定義控件使用attrs屬性”下載

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章