我們來看一個官方View的使用
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:id="@+id/tv" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Hello World!"/></RelativeLayout>自定義屬性的好處就是在xml中去決定屬性,打個比方 這里text 想要做成一個公共的組件就必須把text屬性抽出來,這樣不用每次去java代碼里邊改text。
開始自定義,老規矩創建一個項目,建一個view包,然后再View包下邊創建MyView.class
public class MyView extends View { public MyView(Context context) { this(context,null); } public MyView(Context context, AttributeSet attrs) { this(context, attrs,0); } public MyView(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); init(context,attrs); } PRivate void init(Context context,AttributeSet attrs){ //待處理 }在values里邊創建一個attrs.xml
<?xml version="1.0" encoding="utf-8"?><resources> <declare-styleable name="My"> <attr name="size" format="integer"/> <attr name="color" format="color" /> <attr name="onTouch" format="boolean"/> <attr name="displayStyle" format="integer"> <enum name="normal" value="0"/> <enum name="bold" value="1"/> <enum name="italic" value="2"/> </attr> <attr name="textStyle" format="integer"> <flag name="normal" value="0"/> <flag name="bold" value="1"/> <flag name="italic" value="2"/> </attr> <attr name="shade" format="dimension"/> <attr name="alpha" format="float"/> <attr name="percent" format="fraction"/> <attr name="bg" format="reference"/> <attr name="title" format="string"/> </declare-styleable></resources>name為自定義的名字 fromat是該屬性的類型 android中給提供的format有10種屬性:
//////////////////////////////////////////////////////////////// 自定義View類型/////////////////////////////////////////////////////////// //1,integer 整型 521 //2,color 顏色值 #545454 //3,boolean 布爾型 true/false //4,dimension 單位型 10dp/20px //5,enum 枚舉 *枚舉值 多值選一 //6,flag 標識 *位或運算 多值組合 //7,float 浮點型 1.0f //8,fraction 百分數型 100% //9,reference 資源型 dimens/style/drawable.. //10,string 字符串型 "hello!"private void init(Context context,AttributeSet attrs){ TypedArray arry=context.obtainStyledAttributes(attrs, R.styleable.MyView); //第一種方式 int size=arry.getInteger(R.styleable.MyView_size,0); //第一種方式另一種形態(推薦,官方寫法) final int count=arry.getIndexCount(); for(int i=0;i<count;i++){ int index=arry.getIndex(i); switch (index){ case R.styleable.MyView_size: //如果沒有設置屬性,就給一個default Value int tsize=arry.getInteger(index,0); break; case R.styleable.MyView_color: int color=arry.getColor(index,Color.BLUE); showLog(color+""); break; case R.styleable.MyView_onTouch: boolean normal=arry.getBoolean(index,true); break; case R.styleable.MyView_displayStyle: //enum int displayStyle=arry.getInt(index,0); if(displayStyle!=0){ //證明不是normal } break; case R.styleable.MyView_textStyle: //flag normal|Itsatic int textStyle=arry.getInt(index,0); break; case R.styleable.MyView_shade: int px=arry.getDimensionPixelSize(index ,100); break; case R.styleable.MyView_alpha: float alpha=arry.getFloat(index,1.0f); break; case R.styleable.MyView_percent: //getFraction(int index, int base, int pbase, float defValue) //如果值為10% 則 percent=10%*base //如果值格式為10%p,percent=10%*pbase float percent=arry.getFraction(index,1,1,0); break; case R.styleable.MyView_bg: int resouceId=arry.getResourceId(index ,R.mipmap.ic_launcher); break; case R.styleable.MyView_title: String title=arry.getString(index); showLog(title); break; } } //第二種方法 通過命名空間獲取屬性 String = "http://schemas.android.com/apk/res-auto"; int color = attrs.getAttributeIntValue(space ,"color", Color.BLACK); int size2 = attrs.getAttributeIntValue(space, "size", 10); String title = attrs.getAttributeValue(space, "title"); arry.recycle(); }上面是兩種獲取方法,然后我們在xml中使用
根布局加上命名空間 Andorid Studio: xmlns:my=”http://schemas.android.com/apk/res-auto” Eclipse: xmlns:my=”http://schemas.android.com/apk/res/包名”
<com.tgl.myjni.view.MyView android:layout_alignParentBottom="true" android:layout_width="match_parent" android:layout_height="wrap_content" my:size="24" my:color="#456" my:onTouch="true" my:shade="5dp" my:alpha="0.5" my:percent="100%" my:displayStyle="bold" my:textStyle="bold|normal" my:bg="@mipmap/ic_launcher" my:title="Hello,world!"/>有細心的小伙伴發現了 根本my:根本點不出來 使勁點你也點不出來,這個時候改一個地方,
把這個name值改成和你的自定義View類名保持一致就ok了 沒有的話 編譯一下。 同理 MyView里邊的styleable 也需要修改,run一下
02-28 04:15:38.946 4936-4936/? E/tag: title:Hello,world!02-28 04:15:38.946 4936-4936/? E/tag: alpha:0.502-28 04:15:38.946 4936-4936/? E/tag: color:-1229890602-28 04:15:38.946 4936-4936/? E/tag: size:2402-28 04:15:38.946 4936-4936/? E/tag: onTouch:true02-28 04:15:38.946 4936-4936/? E/tag: displayStyle:102-28 04:15:38.946 4936-4936/? E/tag: textStyle:102-28 04:15:38.946 4936-4936/? E/tag: shade:1802-28 04:15:38.946 4936-4936/? E/tag: percent:1.002-28 04:15:38.946 4936-4936/? E/tag: resouceId:2130903040完畢!
新聞熱點
疑難解答