国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

自定義View-獲取自定義屬性

2019-11-06 09:41:03
字體:
來源:轉載
供稿:網友
大家好,我叫Sincerly ,是一名Android Coder 俗稱碼農,我也是剛剛接觸博客,寫的不好的地方見諒。本篇文章重點是如何在自定義View中創建并使用自定義屬性。 事業無窮年。@dq —— 韓愈

什么是自定義屬性?

我們來看一個官方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>

創建節點 declare-styleable 這個節點將用來存放自定義屬性。然后name命名為My,剛接觸的朋友們 可能會說什么鬼,,下面來逐個解釋:

<attr name="title" format="string">

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

完畢!


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 西充县| 长乐市| 兴宁市| 札达县| 祁门县| 库尔勒市| 海原县| 江北区| 保康县| 合山市| 马鞍山市| 神农架林区| 舞钢市| 东兴市| 沙坪坝区| 砀山县| 富宁县| 西宁市| 临颍县| 叶城县| 岗巴县| 南汇区| 奉新县| 长阳| 辽中县| 芦山县| 中宁县| 诸暨市| 寿光市| 南城县| 金秀| 启东市| 吴川市| 洪雅县| 龙陵县| 栖霞市| 揭东县| 二手房| 环江| 永春县| 沧州市|