1、自绘控件
这个View上所展现的内容全部都是我们自己绘制出来的。绘制的代码是写在onDraw()方法中的。通常自绘控件直接继承自View。
Path、PathMeasure详解中有讲解到自绘控件的例子。
2、组合控件
我们并不需要自己去绘制视图上显示的内容,而只是用系统原生的控件就好了,但我们可以将几个系统原生的控件组合到一起,这样创建出的控件就被称为组合控件。
3、继承控件
我们并不需要自己重头去实现一个控件,只需要去继承一个现有的控件,然后在这个控件上增加一些新的功能,就可以形成一个自定义的控件了,这种自定义控件的特点就是不仅能够按照我们的需求加入相应的功能,还可以保留原生控件的所有功能。
4、事件类控件
通常需要处理触摸事件,并且会消费事件。大多数事件类控件需要结合重回方法来进行,可以给用户的手指有一个良好的反馈。
5、容器类控件
容器类控件一般指 为实现集体的需求 而开发的自定义容器。如 百分比布局,流式布局都是为实现具体摆放的容器类控件,一般实现容器类控件 需要只需要继承五大布局即可。如果满足不了的可以自定义实现。
如下是ToolBar的一个自定义控件:
public class ToolBar extends RelativeLayout {
private ImageView iv_titlebar_left;
private ImageView iv_titlebar_right;
private TextView tv_titlebar_title;
private int mTextColor = Color.WHITE;
private String titlename;
public ToolBar(Context context) {
super(context);
}
public ToolBar(Context context, AttributeSet attrs) {
super(context, attrs);
initView(context, attrs);
}
public ToolBar(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
private void initView(Context context, AttributeSet attrs) {
TypedArray mTypedArray = context.obtainStyledAttributes(attrs, R.styleable.ToolBar);
mTextColor = mTypedArray.getColor(R.styleable.ToolBar_title_text_color, Color.WHITE);
titlename = mTypedArray.getString(R.styleable.ToolBar_title_text);
//获取资源后要及时回收
mTypedArray.recycle();
LayoutInflater.from(context).inflate(R.layout.titlebar, this, true);
iv_titlebar_left = (ImageView) findViewById(R.id.iv_titlebar_left);
iv_titlebar_right = (ImageView) findViewById(R.id.iv_titlebar_right);
tv_titlebar_title = (TextView) findViewById(R.id.tv_titlebar_title);
//设置标题文字颜色
tv_titlebar_title.setTextColor(mTextColor);
setTitle(titlename);
}
public void setLeftListener(OnClickListener onClickListener) {
iv_titlebar_left.setOnClickListener(onClickListener);
}
public void setRightListener(OnClickListener onClickListener) {
iv_titlebar_right.setOnClickListener(onClickListener);
}
public void setTitle(String titlename) {
if (!TextUtils.isEmpty(titlename)) {
tv_titlebar_title.setText(titlename);
}
}
}
标签控件:
public class FlowLayout extends RelativeLayout {
private LayoutInflater mInflater;
private int mWidth;//FlowLayout 的宽度
private List<String> mTags = new ArrayList<String>();
private boolean mInitialized;
Context mContext;
public FlowLayout(Context context) {
this(context, null);
}
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
mWidth = w;
}
public FlowLayout(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public FlowLayout(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
mContext = context;
init(context, attrs, 0);
}
private void init(Context context, AttributeSet attrs, int defStyle) {
mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
ViewTreeObserver mViewTreeObserber = getViewTreeObserver();
mViewTreeObserber.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
if (!mInitialized) {
mInitialized = true;
drawTags();
}
}
});
}
public void addTag(List<String> tags) {
mTags = tags;
drawTags();
}
private void drawTags() {
removeAllViews();
float total = 0;
int index = 1;//现在的位置
int pindex = index;//相对起点位置
for (String item : mTags) {
View tagLayout = (View) mInflater.inflate(R.layout.layout_tag, null);
tagLayout.setId(index);
final TextView tagView = (TextView) tagLayout.findViewById(R.id.tag_txt);
tagView.setText(item);//设置标签view显示的文字
tagView.setPadding(10, 5, 10, 5);
tagView.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(mContext, tagView.getText(), Toast.LENGTH_LONG).show();
}
});
float tagWidth = tagView.getPaint().measureText(item) + 10 * 2;
LayoutParams tagParams = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
if (total + tagWidth < mWidth) {
tagParams.addRule(RelativeLayout.ALIGN_TOP, pindex);
tagParams.addRule(RelativeLayout.RIGHT_OF, index - 1);
if (index > 1) {
tagParams.leftMargin = 10;
total += 10;
}
} else {
tagParams.addRule(RelativeLayout.BELOW, pindex);
tagParams.topMargin = 10;
total = 0;
pindex = index;//
}
total += tagWidth;
addView(tagLayout, tagParams);//添加到相对布局中
index++;
}
}
}
使用:
flowLayout = findViewById(R.id.flow);
List<String> tags = new ArrayList<>();
tags.add("清华大学");
tags.add("北京大学");
tags.add("复旦大学");
tags.add("浙江大学");
tags.add("南开大学");
tags.add("同济大学");
tags.add("苏州大学");
tags.add("吉林大学");
tags.add("哈佛大学");
tags.add("斯坦福大学");
tags.add("麻省理工大学");
tags.add("加州理工学院");
flowLayout.addTag(tags);
网友评论