下面我以一个自定义控件来说明一下它的使用
1.首先要创建变量,创建了个values/attrs.xml文件,文件名任意,但是要在values目录下:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<!--StrechView-->
<declare-styleable name="StretchPanel">
<attr name="contentView" format="reference" />
<attr name="stretchView" format="reference" />
</declare-styleable>
<!--MyButton-->
<declare-styleable name="MyButton">
<attr name="textSize" format="dimension"></attr>
<attr name="textColor" format="color"></attr>
<attr name="text" format="string"></attr>
</declare-styleable>
</resources>
根标签要是resources,定义的变量要有个名字,declare-styleable name="MyButton">,这里定义名称为button。在这个名称里,可以有多个自定义属性。定义了个名为textSize的属性,格式是dimension,这个format指定了textSize属性的类型,只能用于定义字体大小
2.然后是自定义Button:
package com.example.a31210.mybottombarlayout.activity.index;
import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.widget.Button;
import com.example.a31210.mybottombarlayout.R;
/**
* Created by YuShuangPing on 2018/4/13.
*/
public class MyButton extends Button {
public MyButton(Context context) {
super(context);
init(context,null);
}
public MyButton(Context context, AttributeSet attrs) {
super(context, attrs);
init(context,attrs);
}
public MyButton(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init(context,attrs);
}
private void init(Context context, AttributeSet attrs) {
TypedArray typedArray=context.obtainStyledAttributes(attrs, R.styleable.MyButton);
this.setText(typedArray.getString(R.styleable.MyButton_text));
this.setTextSize(typedArray.getDimension(R.styleable.MyButton_textSize,15));
this.setTextColor(typedArray.getColor(R.styleable.MyButton_textColor,getResources().getColor(R.color.colorAccent)));
typedArray.recycle();
}
}
其中,TypedArray实例是个属性的容器,context.obtainStyledAttributes()方法返回得到。AttributeSet是节点的属性集合
3.在布局文件中通过自定义属性赋值:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:stretch="http://schemas.android.com/apk/res-auto"
xmlns:button="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#f4f4f4"
tools:context="com.example.a31210.mybottombarlayout.activity.index.MainActivity">
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<com.example.a31210.mybottombarlayout.activity.index.StretchView
android:id="@+id/stretch_1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="1dp"
stretch:contentView="@layout/view_content_contractdetail_sellerinformation"
stretch:stretchView="@layout/view_stretch_contractdetail_sellerinformation">
</com.example.a31210.mybottombarlayout.activity.index.StretchView>
<com.example.a31210.mybottombarlayout.activity.index.StretchView
android:id="@+id/stretch_2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="1dp"
stretch:contentView="@layout/view_content_contractdetail_buyinformation"
stretch:stretchView="@layout/view_stretch_contractdetail_buyinformation">
</com.example.a31210.mybottombarlayout.activity.index.StretchView>
<com.example.a31210.mybottombarlayout.activity.index.StretchView
android:id="@+id/stretch_3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="1dp"
stretch:contentView="@layout/view_content_contractdetail_recinformation"
stretch:stretchView="@layout/view_stretch_contractdetail_redinformation">
</com.example.a31210.mybottombarlayout.activity.index.StretchView>
<com.example.a31210.mybottombarlayout.activity.index.StretchView
android:id="@+id/stretch_4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="1dp"
stretch:contentView="@layout/view_content_contractdetail_recinformation"
stretch:stretchView="@layout/view_stretch_contractdetail_redinformation2">
</com.example.a31210.mybottombarlayout.activity.index.StretchView>
<com.example.a31210.mybottombarlayout.activity.index.MyButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
button:text="按钮"
button:textSize="18sp"/>
</LinearLayout>
</ScrollView>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="20dp"
android:text="版权归我所有"
android:gravity="center"/>
</LinearLayout>
这里在根标签中增加了:
xmlns:button="http://schemas.android.com/apk/res-auto"
声明了button这个名字空间,myapp是任意的名称,自己可以随便起名,后面的:
"http://schemas.android.com/apk/res-auto"
是固定的
typedArray.getDimension(R.styleable.MyButton_textSize,15);
将获取自定义textSize的值,如果没有,则使用默认的值,15
最后别忘记调用:
typedArray.recycle();