自定義View的時候通常需要提供一些自定義屬性视卢,只需要在res資源目錄的values目錄下創(chuàng)建一個attrs.xml的屬性定義文件礼患,然后在該文件中定義相應的屬性,通過在xml文件引用屬性即可得到相應的數值。
假設自定義VIew:
public class CustomView extends FrameLayout {
public CustomView(@NonNull Context context) {
this(context, null);
}
public CustomView(@NonNull Context context, @Nullable AttributeSet attrs) {
this(context, null, 0);
}
public CustomView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
}
假設在attrs.xml中自定義如下屬性:
<resources>
<!--注意 declare-styleable 的name為自定義View的類名
在xml中使用自定義屬性AS會代碼提示-->
<declare-styleable name="CustomView">
<attr name="customAttribute" format="string" />
</declare-styleable>
</resources>
如圖所示:
attr標簽中的name表示自定義屬性的名稱伪节,format表示自定義屬性的類型(共11種)
- 一、flags
可以并存的屬性值(位或運算) 例:android:configChanges="keyboardHidden|orientation|screenSize"
1绩鸣、在attrs.xml中定義屬性為flags類型:
flag標簽中name代表可選擇的常量架馋,value是常量對應的值(為int類型)
<attr name="x_position" format="flags">
<flag name="left" value="1" />
<flag name="middle" value="10" />
<flag name="right" value="100" />
</attr>
2、xml中使用
如果使用多個屬性全闷,用"|"分割
<!--單個使用-->
app:x_position="left"
<!--多個使用-->
app:x_position="left|right"
3叉寂、在自定義View的構造函數中獲取屬性的值。
獲取到的int值為設置的屬性值的和
比如:app:x_position="left|right"
position值為:1left對應的value
+100 right對應的value
=101总珠;
如果未設置為0屏鳍;
根據值的總和去判斷用戶設置的是什么常量。
TypedArray array = getContext().obtainStyledAttributes(attrs,R.styleable.CustomView);
int position = array.getInt(R.styleable.CustomView_x_position, 0);
array.recycle();
- 二局服、dimension
尺寸類型值 例: android:paddingLeft="10dp" android:paddingRight="@dimen/dp_10"
1钓瞭、在attrs.xml中定義屬性為dimension類型:
<attr name="x_text_size" format="dimension"/>
2、xml中使用
<!--直接使用尺寸數值 如 10dp 10px-->
app: x_text_size ="10dp"
app: x_text_size ="10sp"
app: x_text_size ="10px"
<!--引用dimen文件中的資源-->
app: x_text_size ="@dimen/x_20dp"
3淫奔、在自定義View的構造函數中獲取屬性的值山涡。
獲取到float類型的值
TypedArray array = getContext().obtainStyledAttributes(attrs,R.styleable.CustomView);
float textSize = array.getDimension(R.styleable.CustomView_x_text_size, 0);
array.recycle();
- 三、color
顏色類型值 例: android:background="#000"
1唆迁、在attrs.xml中定義屬性為color類型:
<attr name="x_text_color" format="color"/>
2鸭丛、xml中使用
<!--直接使用顏色數值 如 #fff-->
app:x_text_color="#fff"
<!--引用color文件中的資源-->
app:x_text_color="@color/colorAccent"
3、在自定義View的構造函數中獲取屬性的值唐责。
獲取到int類型的值
TypedArray array = getContext().obtainStyledAttributes(attrs,R.styleable.CustomView);
int color = array.getColor(R.styleable..CustomView_x_text_color, getResources().getColor(android.R.color.darker_gray));
array.recycle();
- 四鳞溉、string
字符串類型值 例: android:text="java"
1、在attrs.xml中定義屬性為string類型:
<attr name="x_text" format="string"/>
2鼠哥、xml中使用
<!--直接使用 如 java-->
app:x_text="Java"
<!--引用string文件中的資源-->
app:x_text="@string/app_name"
3熟菲、在自定義View的構造函數中獲取屬性的值。
獲取到String類型的值
TypedArray array = getContext().obtainStyledAttributes(attrs,R.styleable.CustomView);
String string = array.getString(R.styleable.CustomView_x_text);
array.recycle();
- 五朴恳、reference
引用類型值 例: android:src="@mipmap/ic_launcher"
1抄罕、在attrs.xml中定義屬性為reference類型:
<attr name="x_src" format="reference"/>
2、xml中使用
<!--直接使用資源-->
app:x_src="@mipmap/ic_launcher"
app:x_src="@array/sports"
3于颖、在自定義View的構造函數中獲取屬性的值呆贿。
獲取到資源的值
TypedArray array = getContext().obtainStyledAttributes(attrs,R.styleable.CustomView);
int resourceId = array.getResourceId(R.styleable.CustomView.x_src, 0);
// 根據具體情況通過資源id拿到對應的value
Drawable drawable = getResources().getDrawable(resourceId);
String[] stringArray = getResources().getStringArray(resourceId);
array.recycle();
- 六、boolean
布爾類型值 例: android:layout_centerInParent="true"
1恍飘、在attrs.xml中定義屬性為boolean類型:
<attr name="x_center" format="boolean"/>
2榨崩、xml中使用
app: x_center ="true"
3谴垫、在自定義View的構造函數中獲取屬性的值。
獲取到布爾值
TypedArray array = getContext().obtainStyledAttributes(attrs,R.styleable.CustomView);
boolean center = array.getBoolean(R.styleable.CustomView_x_center, false);
array.recycle();
- 七母蛛、enum
枚舉類型值 例: android:gravity="center"
1翩剪、在attrs.xml中定義屬性為enum類型:
<attr name="x_location" format="enum">
<enum name="left" value="0" />
<enum name="right" value="1" />
<enum name="top" value="2" />
<enum name="bottom" value="3" />
<enum name="center" value="4" />
</attr>
2、xml中使用
app: x_location ="left"
3彩郊、在自定義View的構造函數中獲取屬性的值前弯。
獲取到int值
TypedArray array = getContext().obtainStyledAttributes(attrs,R.styleable.CustomView);
boolean center = array.getBoolean(R.styleable.CustomView_x_location, 0);
array.recycle();
- 八、fraction
百分比類型值
1秫逝、在attrs.xml中定義屬性為fraction類型:
<attr name="x_alpha" format="fraction" />
2恕出、xml中使用
<!--相對于自身基準值-->
app: x_alpha ="10%"
<!--相對于父容器基準值-->
app: x_alpha ="10%p"
3、在自定義View的構造函數中獲取屬性的值违帆。
獲取到float值(10% 自身基準值【1】 為0.1 浙巫,10%p 父容器基準值【2】 為0.2)
TypedArray array = getContext().obtainStyledAttributes(attrs,R.styleable.CustomView);
// 第二個參數為自身基準值,第三個參數為父容器基準值
float alpha = array.getFraction(R.styleable.CustomView_x_alpha, 1, 2, 1);
array.recycle();
- 十一刷后、混合類型
屬性定義時可以指定多種類型的值 用"|"分開
比如需要設置背景既可以是顏色或者是一張圖片
1的畴、在attrs.xml中定義屬性為color|reference類型:
<attr name="x_background" format="color|reference"/>
2、xml中使用
<!--引用圖片資源-->
app:x_background="@drawable/serach_bg"
<!--直接使用color-->
app:x_background="#fff"
3尝胆、在自定義View的構造函數中獲取屬性的值丧裁。
獲取到drawable
TypedArray array = getContext().obtainStyledAttributes(attrs,R.styleable.CustomView);
Drawable drawable = array.getDrawable(R.styleable.CustomView_x_background);
array.recycle();