گ 我就是我我只為自己代言 گ
1.使用xml檔案可以快速有效的定義我們app的介面,可是有時候官方定義的一些基本元件不夠用,於是我們就需要自定義元件來滿足我們的開發需求,因此,我們就藉助attrs.xml來實現,改目錄位於res/values目錄:下面是studio建立attrs.xml
<?xml version="1.0" encoding="utf-8"?> <resources> <!--是一個declare-styleable組,這個組名字myStudy(想寫什麼寫什麼最好顧名思義),後面佈局檔案中會用到, attr標籤是定義屬性名字以及屬性值 --> <declare-styleablename="myStudy"> <attr name="textColor" format="color"></attr> <attr name="textSize" format="dimension"></attr> </declare-styleable> <!--是一個declare-styleable組,也可以不要,直接寫attr屬性 --> </resources>
2.簡單寫一下自定義的myView:
public class MyView extendsView { private Paint mPaint; private static final String mString = "Study Custom Attrs !!!!"; public MyView(Context context, int[] myStudy) { super(context); } public MyView(Context context, @Nullable AttributeSet attrs) { super(context, attrs); mPaint = new Paint();//TypedArray是一個數組容器:作用就是資源的對映作用 TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.myStudy); //設定TextView的各個屬性值 float textSize = ta.getDimension(R.styleable.myStudy_textSize , 30); int textColor = ta.getColor(R.styleable.myStudy_textColor , 0xFFFFFFF); //為畫筆設定各個屬性值 mPaint.setTextSize(textSize); mPaint.setColor(textColor); //釋放記憶體回收TypedArray, ta.recycle(); } @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); mPaint.setColor(Color.RED); //設定填充 // Paint.Style.FILL :填充內部 // Paint.Style.FILL_AND_STROKE :填充內部和描邊 // Paint.Style.STROKE :僅描邊 mPaint.setStyle(Paint.Style.FILL); //繪製矩形控制元件:左、上、右 、底 canvas.drawRect(new Rect(10 , 10 ,310 , 100) ,mPaint); //畫筆顏色 mPaint.setColor(Color.BLUE); //繪製文字:座標X10 ,y100 canvas.drawText(mString, 20, 65 ,mPaint); }
3.佈局檔案:
<?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:myStudy="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.example.wangqing.studyattrs.MainActivity"> <com.example.wangqing.studyattrs.MyView android:id="@+id/id_my" android:layout_width="wrap_content" android:layout_height="wrap_content" myStudy:textSize="14sp" myStudy:textColor="#ff22ff" /> </RelativeLayout>
佈局檔案xmlns:myStudy="http://schemas.android.com/apk/res-auto"就是對我們自定義的attrs引入,
myStudy:textSize="14sp" myStudy:textColor="#ff22ff"屬性就是我們attrs定義的屬性
4.進入知識拓展:
001.兩個styleable,同時包含了相同的屬性custom,這時在編譯時會提示Attribute “xxx” has already been defined,表示相同屬性重複定義:a.相同styleable name不能再同一個attr.xml中重複定義,b.styleable name不一致attir name也不能重複定義,attr format屬性不影響重複定義結果。因此可以採用如下方法解決該問題
<declare-styleable name="Sample">
<attr name="custom" format="string|reference" />
</declare-styleable>
<declare-styleable name="Sample1">
<attr name="custom" format="string|reference" />
</declare-styleable>
<attr name="custom" format="string|reference" />
<declare-styleable name="Sample">
<attr name="custom" />
</declare-styleable>
<declare-styleable name="Sample1">
<attr name="custom" />
</declare-styleable>
002.AttributeSet與TypedArray
構造方法中的有個引數叫做AttributeSet(eg: MyView(Context context, AttributeSet attrs) )這個引數看名字就知道包含的是引數的集合,那麼我能不能通過它去獲取我的自定義屬性呢?
首先AttributeSet中的確儲存的是該View宣告的所有的屬性,並且外面的確可以通過它去獲取(自定義的)屬性,怎麼做呢?
其實看下AttributeSet的方法就明白了,下面看程式碼。
public MyView(Context context, AttributeSet attrs) {
super(context, attrs);
TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.test);
String text = ta.getString(R.styleable.test_testAttr);
int textAttr = ta.getInteger(R.styleable.test_text, -1);
Log.e(TAG, "text = " + text + " , textAttr = " + textAttr);
ta.recycle();
}
public MyTextView(Context context, AttributeSet attrs) {
super(context, attrs);
int count = attrs.getAttributeCount();
for (int i = 0; i < count; i++) {
String attrName = attrs.getAttributeName(i);
String attrVal = attrs.getAttributeValue(i);
Log.e(TAG, "attrName = " + attrName + " , attrVal = " + attrVal);
}
// ==>use typedarray ...
}
但是,在
android:layout_width="@dimen/dp100"
android:layout_height="@dimen/dp200"
這種情況時AttributeSet取得值就是一個不知道的東西,而TypedArray依然可以取得對應的值,所以TypedArray是用來簡化我們的工作的,再次建議大家使用TypedArray 003.Android自定義屬性詳解(網路貼上):
1. reference:參考某一資源ID。
(1)屬性定義:
<declare-styleable name = "名稱">
<attr name = "background" format = "reference" />
</declare-styleable>
(2)屬性使用:
<ImageView
android:layout_width = "42dip"
android:layout_height = "42dip"
android:background = "@drawable/圖片ID"
/>
2. color:顏色值。
(1)屬性定義:
<declare-styleable name = "名稱">
<attr name = "textColor" format = "color" />
</declare-styleable>
(2)屬性使用:
<TextView
android:layout_width = "42dip"
android:layout_height = "42dip"
android:textColor = "#00FF00"
/>
3. boolean:布林值。
(1)屬性定義:
<declare-styleable name = "名稱">
<attr name = "focusable" format = "boolean" />
</declare-styleable>
(2)屬性使用:
<Button
android:layout_width = "42dip"
android:layout_height = "42dip"
android:focusable = "true"
/>
4. dimension:尺寸值。
(1)屬性定義:
<declare-styleable name = "名稱">
<attr name = "layout_width" format = "dimension" />
</declare-styleable>
(2)屬性使用:
<Button
android:layout_width = "42dip"
android:layout_height = "42dip"
/>
5. float:浮點值。
(1)屬性定義:
<declare-styleable name = "AlphaAnimation">
<attr name = "fromAlpha" format = "float" />
<attr name = "toAlpha" format = "float" />
</declare-styleable>
(2)屬性使用:
<alpha
android:fromAlpha = "1.0"
android:toAlpha = "0.7"
/>
6. integer:整型值。
(1)屬性定義:
<declare-styleable name = "AnimatedRotateDrawable">
<attr name = "visible" />
<attr name = "frameDuration" format="integer" />
<attr name = "framesCount" format="integer" />
<attr name = "pivotX" />
<attr name = "pivotY" />
<attr name = "drawable" />
</declare-styleable>
(2)屬性使用:
<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"
/>
7. string:字串。
(1)屬性定義:
<declare-styleable name = "MapView">
<attr name = "apiKey" format = "string" />
</declare-styleable>
(2)屬性使用:
<com.google.android.maps.MapView
android:layout_width = "fill_parent"
android:layout_height = "fill_parent"
android:apiKey = "0jOkQ80oD1JL9C6HAja99uGXCRiS2CGjKO_bc_g"
/>
8. fraction:百分數。
(1)屬性定義:
<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>
(2)屬性使用:
<rotate xmlns:android = "http://schemas.android.com/apk/res/android"
android:interpolator = "@anim/動畫ID"
android:fromDegrees = "0"
android:toDegrees = "360"
android:pivotX = "200%"
android:pivotY = "300%"
android:duration = "5000"
android:repeatMode = "restart"
android:repeatCount = "infinite"
/>
9. enum:列舉值。
(1)屬性定義:
<declare-styleable name="名稱">
<attr name="orientation">
<enum name="horizontal" value="0" />
<enum name="vertical" value="1" />
</attr>
</declare-styleable>
(2)屬性使用:
<LinearLayout
xmlns:android = "http://schemas.android.com/apk/res/android"
android:orientation = "vertical"
android:layout_width = "fill_parent"
android:layout_height = "fill_parent"
>
</LinearLayout>
10. flag:位或運算。
(1)屬性定義:
<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>
(2)屬性使用:
<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>
注意:
屬性定義時可以指定多種型別值。
(1)屬性定義:
<declare-styleable name = "名稱">
<attr name = "background" format = "reference|color" />
</declare-styleable>
(2)屬性使用:
<ImageView
android:layout_width = "42dip"
android:layout_height = "42dip"
android:background = "@drawable/圖片ID|#00FF00"
/>