1在values目录下新建attrs文件
2在构造器中通过TypedArray获取属性
3给控件设置属性值
4在代码中使用
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="MyToggleView">//属性集名称,指定这些属性是属于哪个控件的
<attr name="circle_color" format="color"/>//声明属性格式
<attr name="offbackground_color" format="color"/>
<attr name="onbackground_color" format="color"/>
<attr name="toggle" format="boolean"></attr>
</declare-styleable>
<declare-styleable name="TopBar">
<attr name="topbartitle" format="string"/>
<attr name="titleTextSize" format="dimension"/>
<attr name="topbartitleTextColor" format="color"/>
<attr name="leftTextColor" format="color"/>
<attr name="leftBackground" format="color|reference"/>
<attr name="leftText" format="string"/>
<attr name="rightTextColor" format="color"/>
<attr name="rightBackground" format="color|reference"/>
<attr name="rightText" format="string"/>
</declare-styleable>
</resources>
所有的format类型
reference 引用
color 颜色
boolean 布尔值
dimension 尺寸值
float 浮点值
integer 整型值
string 字符串
enum 枚举值
package tripcare.qianniu.com.myview.widget;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.TextView;
import tripcare.qianniu.com.myview.R;
/**
* Created by chenmeng on 2016/11/4.
*/
public class TopBar extends RelativeLayout {
private Button mLeftButton;
private Button mRightButton;
private TextView mTitleView;
private LayoutParams mLeftParams;
private LayoutParams mRightParams;
private LayoutParams mTitleParams;
private int mLeftTextColor;
private Drawable mLeftBackground;
private String mLeftText;
private int mRightTextColor;
private Drawable mRightBackground;
private String mRightText;
private float mTitleTextSize;
private int mTitleTextColor;
private String mTitle;
private topbarClickListener listener;
public TopBar(Context context) {
super(context);
}
public TopBar(Context context, AttributeSet attrs) {
super(context, attrs);
TypedArray ta=context.obtainStyledAttributes(attrs, R.styleable.TopBar);
mLeftTextColor=ta.getColor(R.styleable.TopBar_leftTextColor,0);
mLeftBackground=ta.getDrawable(R.styleable.TopBar_leftBackground);
mLeftText=ta.getString(R.styleable.TopBar_leftText);
mRightTextColor=ta.getColor(R.styleable.TopBar_rightTextColor,0);
mRightBackground=ta.getDrawable(R.styleable.TopBar_rightBackground);
mRightText=ta.getString(R.styleable.TopBar_rightText);
mTitleTextSize=ta.getDimension(R.styleable.TopBar_titleTextSize,10);
mTitleTextColor=ta.getColor(R.styleable.TopBar_topbartitleTextColor,0);
mTitle=ta.getString(R.styleable.TopBar_topbartitle);
//recycle方法来避免重新创建时的错误
ta.recycle();
mLeftButton=new Button(context);
mRightButton=new Button(context);
mTitleView = new TextView(context);
//赋值
mLeftButton.setTextColor(mLeftTextColor);
mLeftButton.setBackground(mLeftBackground);
mLeftButton.setText(mLeftText);
mRightButton.setTextColor(mRightTextColor);
mRightButton.setBackground(mRightBackground);
mRightButton.setText(mRightText);
mTitleView.setTextColor(mTitleTextColor);
mTitleView.setText(mTitle);
mTitleView.setTextSize(mTitleTextSize);
mTitleView.setGravity(Gravity.CENTER);
//布局元素
mLeftParams=new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.MATCH_PARENT);
mLeftParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT,TRUE);
addView(mLeftButton,mLeftParams);
mRightParams=new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.MATCH_PARENT);
mRightParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT,TRUE);
addView(mRightButton,mRightParams);
mTitleParams=new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.MATCH_PARENT);
mTitleParams.addRule(RelativeLayout.CENTER_IN_PARENT,TRUE);
addView(mTitleView,mTitleParams);
mRightButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
listener.reghtClick();
}
});
mLeftButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
listener.leftClick();
}
});
}
public TopBar(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
public interface topbarClickListener{
void leftClick();
void reghtClick();
}
public void setTopbarClickListener(topbarClickListener listener){
this.listener=listener;
}
}
在代码中使用
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
//安卓Studio命名空间,topbar可任意更改为其他名称
xmlns:topbar="http://schemas.android.com/apk/res-auto"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<tripcare.qianniu.com.myview.widget.TopBar
android:id="@+id/topbar"
android:layout_width="match_parent"
android:layout_height="40dp"
topbar:leftText="返回"
topbar:leftBackground="@mipmap/ic_brows_back"
topbar:leftTextColor="@color/colorPrimary"
topbar:rightBackground="@mipmap/home"
topbar:rightText="主页"
topbar:titleTextSize="12sp"
topbar:topbartitle="定位"
topbar:topbartitleTextColor="@color/colorPrimary"></tripcare.qianniu.com.myview.widget.TopBar>
</LinearLayout>