在开发的时候,我们有时候会遇到这么一种情况,就是某种特性都一致,而且可以重复使用,并且有些属性安卓原生没有,我们这时候就可以自己自定义安卓的属性view,其实说白了,在原码里也,处处有这样的属性view(用到的就是这个东西),如下:
TypedArray a = context.obtainStyledAttributes();
,比如Button,我们在源码中也可以看到使用这个最基本的属性,其继承TextView,里面的大量属性,比如,颜色、字体大小,字体颜色等等。都是用的这个来实现的,我们在layout文件里只需设置textColor:XXX,textSize:XXX,这些属性,系统就可以为我们自动设置了这些属性。源码是:
public TextView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
····
TypedArray a = theme.obtainStyledAttributes(attrs,
com.android.internal.R.styleable.TextViewAppearance, defStyleAttr, defStyleRes);
···
switch (attr) {
···
case com.android.internal.R.styleable.TextAppearance_textColor:
textColor = appearance.getColorStateList(attr);
break;
case com.android.internal.R.styleable.TextAppearance_textColorHint:
textColorHint = appearance.getColorStateList(attr);
break;
···
}
}
case 里的值,比如textColor、textColorHint等这些值都是在:
android_sdk_path/platforms/android-22/data/res/values/attrs里可以找到,我们看到这里有很多的属性可供我们来用。
具体怎么怎用这里属性,我们接下来就通过一个小例子来实现一下。
1、我们要实现我们自定义的属性view,我们首先得有一份文件,得记录我们都有哪些属性,一般我们都是在res/values/attrs.xml下添加我们需要的东西(没有这个文件,需要自己先创建下);而且我们用declare-styleable标签来管理我们的属性组attr。比如,我们定义了以下的以下属性:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="CombinationViewItem_styleable">
<attr name="icon" format="reference"></attr>
<attr name="title" format="string"></attr>
<attr name="num" format="inteter"></attr>
<attr name="isShow" format="boolean"></attr>
<attr name="desColor" format="color"></attr>
<attr name="layout" format="reference"></attr>
</declare-styleable>
</resources>
这里的name值都是自定义的一些属性key。我们在给属性赋值的时候就是用这个来一一对应值的。后面的format就是我们要实现的value是什么类型的。
这里我们用的值都是原始数据类型,当然有些小出入,都是小写。
其类型一般有:
string —-字符串
reference —-资源文件
boolean —- boolean值
dimension —-字体大小值
color —-颜色值
integer —-int型值
我们在上面的例子里各弄一个,来显示我们的自定义的view,最后一个是显示的一个layout.xml。
2、有了自定义的属性文件,我们接下来就是要实现我们的重头戏了,把这些值都变成现实。
首先我们先创建一个文件view.
public class CombinationViewItem extends LinearLayout{
private Context mContext;
public CombinationViewItem(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public CombinationViewItem(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
mContext = context;
TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.CombinationViewItem_styleable);
int iconId = a.getResourceId(R.styleable.CombinationViewItem_styleable_icon, 0);
String key = a.getString(R.styleable.CombinationViewItem_styleable_key);
String title = a.getString(R.styleable.CombinationViewItem_styleable_title);
String des = a.getString(R.styleable.CombinationViewItem_styleable_des);
int desColor = a.getColor(R.styleable.CombinationViewItem_styleable_desColor, getResources().getColor(R.color.oriage));
boolean isShowLine = a.getBoolean(R.styleable.CombinationViewItem_styleable_isShowLine, true);
a.recycle();
View v = LayoutInflater.from(context).inflate(R.layout.index_1_item, null);
ImageView icon = (ImageView) v.findViewById(R.id.item_img);
TextView titleTv = (TextView) v.findViewById(R.id.item_mid_tv);
TextView desTv = (TextView) v.findViewById(R.id.item_right_tv);
View line = v.findViewById(R.id.devider_line);
titleTv.setText(title);
desTv.setText(des);
desTv.setTextColor(desColor);
icon.setImageResource(iconId);
if (!isShowLine) {
line.setVisibility(GONE);
}
addView(v);
v.setTag(key);
}
}
3、在准备工作完成之后,我们就可以引用自定义的view了。准备一个layout:test.xml.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<com.android.test.view.CombinationViewItem
android:layout_width="match_parent"
android:layout_height="50dp"
app:des="软件签到"
app:desColor="@color/light_gray"
app:icon="@drawable/icon"
app:key="one"
app:title="已安装软件签到"/>
<com.android.xiaocaimi.view.CombinationViewItem
android:layout_width="match_parent"
android:layout_height="50dp"
app:des="赚钱就这么容易"
app:desColor="@color/light_gray"
app:icon="@drawable/icon"
app:key="one"
app:title="广告红包"/>
</LinearLayout>
在这里要要注意下属性引用:
xmlns:app="http://schemas.android.com/apk/res-auto"
app是我们自定义的,这个名字可以随便取。在定义属性的时候,我们就应该以这个开头,在用attr下对应的属性名来定义不同的属性就OK了。
所有的准备工作做好后运行程序:我们就可以得到如下的效果了。
补上一个layout文件:index_1_item.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="50dp">
<ImageView
android:id="@+id/item_img"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:padding="2dp"
android:scaleType="centerInside" />
<TextView
android:id="@+id/item_right_tv"
android:layout_width="wrap_content"
android:layout_height="50dp"
android:layout_alignBottom="@+id/item_img"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:drawableRight="@drawable/arrow_right_myinfo"
android:gravity="center"
android:text="做任务赚钱"
android:textColor="@color/oriage" />
<TextView
android:id="@+id/item_mid_tv"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_marginLeft="10dp"
android:layout_toLeftOf="@id/item_right_tv"
android:layout_toRightOf="@id/item_img"
android:gravity="left|center_vertical"
android:text="123" />
<View
android:id="@+id/devider_line"
android:layout_width="match_parent"
android:layout_height="1px"
android:layout_alignParentBottom="true"
android:layout_marginLeft="50dp"
android:layout_marginRight="15dp"
android:background="@color/gray" />
</RelativeLayout>