1.设置属性(供XML调用)

在res目录新建attrs.xml文件

<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="MyTopBar">
<attr name="title" format="string"/>
<attr name="titleSize" format="dimension"/>
<attr name="titleColor" format="color"/> <attr name="leftButtonText" format="string"/>
<attr name="leftButtonTextSize" format="dimension"/>
<attr name="leftButtonTextColor" format="color"/>
<attr name="leftButtonTextBackGround" format="reference|color"/> <attr name="rightButtonText" format="string"/>
<attr name="rightButtonTextSize" format="dimension"/>
<attr name="rightButtonTextColor" format="color"/>
<attr name="rightButtonTextBackGround" format="reference|color"/> <attr name="rightButtonText2" format="string"/>
<attr name="rightButtonTextSize2" format="dimension"/>
<attr name="rightButtonTextColor2" format="color"/>
<attr name="rightButtonTextBackGround2" format="reference|color"/>
</declare-styleable>
</resources>

2.创建自定义继承于(view/viewGroup/各种组件)

public class MyTopBar extends RelativeLayout {
//TopBar容器包含的组件
private Button mLeftButton, mRightButton, mRightButton2;
private TextView mTitleTextView; // 布局属性,用来控制组件元素在ViewGroup中的位置
private LayoutParams mLeftParams, mTitleParams, mRightParams, mRightParams2; //定义title属性
private String mTitle;
private int mTitleColor;
private float mTitleSize;
//定义leftButton属性
private String mLeftButtonText;
private float mLeftButtonTextSize;
private int mLeftButtonTextColor;
private Drawable mLeftButtonTextBackGround;
//定义最右边的RightButton属性
private String mRightButtonText;
private float mRightButtonTextSize;
private int mRightButtonTextColor;
private Drawable mRightButtonTextBackGround;
//定义倒数第二个rightButton的属性
private String mRightButtonText2;
private float mRightButtonTextSize2;
private int mRightButtonTextColor2;
private Drawable mRightButtonTextBackGround2; // 映射传入的接口对象
private topBarClickListener mListener; public MyTopBar(Context context) {
super(context);
} //这个构造函数是包含attrs的,XML在加载时候会调用这个构造函数
public MyTopBar(Context context, AttributeSet attrs) {
super(context, attrs);
/*
1.把所有的attrs的值添加到TypeArray中
*/
TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.MyTopBar);
//把TypeArray中的值取出来
mTitle = typedArray.getString(R.styleable.MyTopBar_title);
mTitleColor = typedArray.getColor(R.styleable.MyTopBar_titleColor, 0);
mTitleSize = typedArray.getDimension(R.styleable.MyTopBar_titleSize, 10); mLeftButtonText = typedArray.getString(R.styleable.MyTopBar_leftButtonText);
mLeftButtonTextColor = typedArray.getColor(R.styleable.MyTopBar_leftButtonTextColor, 0);
mLeftButtonTextSize = typedArray.getDimension(R.styleable.MyTopBar_leftButtonTextSize, 10);
mLeftButtonTextBackGround = typedArray.getDrawable(R.styleable.MyTopBar_leftButtonTextBackGround); mRightButtonText = typedArray.getString(R.styleable.MyTopBar_rightButtonText);
mRightButtonTextColor = typedArray.getColor(R.styleable.MyTopBar_rightButtonTextColor, 0);
mRightButtonTextSize = typedArray.getDimension(R.styleable.MyTopBar_rightButtonTextSize, 10);
mRightButtonTextBackGround = typedArray.getDrawable(R.styleable.MyTopBar_rightButtonTextBackGround); mRightButtonText2 = typedArray.getString(R.styleable.MyTopBar_rightButtonText2);
mRightButtonTextColor2 = typedArray.getColor(R.styleable.MyTopBar_rightButtonTextColor2, 0);
mRightButtonTextSize2 = typedArray.getDimension(R.styleable.MyTopBar_rightButtonTextSize2, 10);
mRightButtonTextBackGround2 = typedArray.getDrawable(R.styleable.MyTopBar_rightButtonTextBackGround2);
//取完不要忘记recycle,防止下次调用失败
typedArray.recycle(); /*
2.组合控件
*/
mTitleTextView = new TextView(context);
mLeftButton = new Button(context);
mRightButton = new Button(context);
mRightButton2 = new Button(context); //为元素添加设置属性,来自XML的值会自动在这里进行设置
mTitleTextView.setText(mTitle);
mTitleTextView.setTextColor(mTitleColor);
mTitleTextView.setTextSize(TypedValue.COMPLEX_UNIT_SP,mTitleSize); mLeftButton.setText(mLeftButtonText);
mLeftButton.setTextSize(TypedValue.COMPLEX_UNIT_SP,mLeftButtonTextSize);
mLeftButton.setTextColor(mLeftButtonTextColor);
mLeftButton.setBackground(mLeftButtonTextBackGround);
mLeftButton.setId(R.id.topBar_left_button); mRightButton.setText(mRightButtonText);
mRightButton.setTextColor(mRightButtonTextColor);
mRightButton.setTextSize(TypedValue.COMPLEX_UNIT_SP,mRightButtonTextSize);
mRightButton.setBackground(mRightButtonTextBackGround);
mRightButton.setId(R.id.topBar_right_button);//这个id需要在res文件中添加ids.xml文件 mRightButton2.setText(mRightButtonText2);
mRightButton2.setTextColor(mRightButtonTextColor2);
mRightButton2.setTextSize(TypedValue.COMPLEX_UNIT_SP,mRightButtonTextSize2);
mRightButton2.setBackground(mRightButtonTextBackGround2);
//为组件设置布局
mLeftParams = new LayoutParams(
200,
ViewGroup.LayoutParams.WRAP_CONTENT);
mLeftParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT,TRUE);//居于父布局左侧
addView(mLeftButton,mLeftParams);//添加到ViewGroup mRightParams = new LayoutParams(
200,
ViewGroup.LayoutParams.MATCH_PARENT);
mRightParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT,TRUE);//居于父布局右侧
addView(mRightButton,mRightParams); mRightParams2 = new LayoutParams(
//px = dp*(dpi/160)这里是像素
80,
80);
mRightParams2.addRule(RelativeLayout.LEFT_OF,R.id.topBar_right_button);//居于最右侧按键左侧
mRightParams2.addRule(RelativeLayout.CENTER_VERTICAL,TRUE);
addView(mRightButton2,mRightParams2); mTitleParams = new LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
mTitleParams.addRule(RelativeLayout.CENTER_IN_PARENT,TRUE);
mTitleParams.addRule(RelativeLayout.RIGHT_OF,R.id.topBar_left_button);//居于最左侧按键右侧
addView(mTitleTextView,mTitleParams); //定义事件监听
mLeftButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
mListener.leftButtonClick();
}
}); mRightButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
mListener.RightButtonClick();
}
}); mRightButton2.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
mListener.RightButtonClick2();
}
});
} /*
3.定义接口,留给调用者实现
*/
public interface topBarClickListener {
//左按键
void leftButtonClick();
//右按键
void RightButtonClick();
//右按键2
void RightButtonClick2();
} // 暴露一个方法给调用者来注册接口回调
// 通过接口来获得回调者对接口方法的实现
public void setOnTopBarClickListener(topBarClickListener mListener) {
this.mListener = mListener;
}
}

3.可见性设置

   /*
4.高度自定义
*/
public void setButtonVisible(int id, boolean flag) {
if (flag) {
if (id == 0) {
mLeftButton.setVisibility(View.VISIBLE);
} else if (id == 1) {
mRightButton.setVisibility(View.VISIBLE);
} else {
mRightButton2.setVisibility(View.VISIBLE);
}
} else {
if (id == 0) {
mLeftButton.setVisibility(View.GONE);
} else if (id == 1) {
mRightButton.setVisibility(View.GONE);
} else {
mRightButton2.setVisibility(View.GONE);
}
}
}

自定义View/ViewGroup的步骤和实现的更多相关文章

  1. 【Android - 自定义View】之自定义View浅析

    1.概述 Android自定义View / ViewGroup的步骤大致如下: 1) 自定义属性: 2) 选择和设置构造方法: 3) 重写onMeasure()方法: 4) 重写onDraw()方法: ...

  2. 自定义View完全解析

    自定义View主要包括以下3种方式: 一.组合控件,利用已有控件的组合,来满足自己的需求. 例子:顶部导航栏 二.继承已有View,比如继承TextView.ImageView等,根据需要重写相应的方 ...

  3. 自定义View分类与流程

    自定义View分类与流程(进阶篇)## 转载出处: http://www.gcssloop.com/customview/CustomViewProcess/ 自定义View绘制流程函数调用链(简化版 ...

  4. Android 自定义view(二) —— attr 使用

    前言: attr 在前一篇文章<Android 自定义view -- attr理解>已经简单的进行了介绍和创建,那么这篇文章就来一步步说说attr的简单使用吧 自定义view简单实现步骤 ...

  5. Android查缺补漏(View篇)--自定义View利器Canvas和Paint详解

    上篇文章介绍了自定义View的创建流程,从宏观上给出了一个自定义View的创建步骤,本篇是上一篇文章的延续,介绍了自定义View中两个必不可少的工具Canvas和Paint,从细节上更进一步的讲解自定 ...

  6. 自定义View的实现流程

    1.继承View组件,比如,LabelView继承了View   2.重写两个构造方法,比如,对于自定义View LabelView   LabelView(Context context),如果该自 ...

  7. [原] Android 自定义View步骤

    例子如下:Android 自定义View 密码框 例子 1 良好的自定义View 易用,标准,开放. 一个设计良好的自定义view和其他设计良好的类很像.封装了某个具有易用性接口的功能组合,这些功能能 ...

  8. 自定义View系列教程07--详解ViewGroup分发Touch事件

    深入探讨Android异步精髓Handler 站在源码的肩膀上全解Scroller工作机制 Android多分辨率适配框架(1)- 核心基础 Android多分辨率适配框架(2)- 原理剖析 Andr ...

  9. 自定义的ViewGroup中添加自定义View 造成的无法显示问题(个人)

    首先说一下我在网上找了很久没有找到说明方法所以我就自己试着写了一下 1.我自定义了一个继承了RelativeLayout的ViewGroup java代码如下: /** * 简单自定义三头像 * @a ...

随机推荐

  1. springcloud 之 feign的重复性调用 优化

    最近有一个springcloud的feign请求,用于获取坐标经纬度的信息,返回结果永远是固定不变的,所以考虑优化一下,不然每次转换几个坐标都要去请求feign,返回的所有坐标信息,数据量太大导致耗时 ...

  2. 用weex create 创建app项目 报 ERROR in index.web.js from UglifyJs 错误!

    用weex create创建一个APP项目,安装依赖后运行报 这个是package.json index.web.js 在dist目录下是build时生成的. 上面的答案没有给大家细节,不好意思致歉下 ...

  3. 12个非常不错的javascript类库

    Javascript是一个解释性的编程语言.最初作为浏览器的一部份在浏览器中运行,可以和用户交互,并且控制浏览器,异步通讯,修改显示的document.在这篇文章中,我们收集了12款最新的Javasc ...

  4. MySQL使用一张表的一列更新另一张表的一列

    使用MySQL中,在一张表etl_table_field_info上新增了一个字段tgt_table_en_name,该字段的值想从表etl_table_property_info的tgt_table ...

  5. Hyper-V 2016 配置管理系列(准备篇)

    2.1 推荐软硬件配置 2.2 Hyper主机前提准备 前提条件: 具有二级地址转换(SLAT)的64位处理器.要安装Hyper-V虚拟化组件(如Windows管理程序),处理器必须具有SLAT 足够 ...

  6. 【文件拷贝】使用Total Commander Portable拖动拷贝文件,支持队列

    使用Total Commander Portable,可以批量拷贝多个位于相同或不同目录的文件(夹)到指定的相同或不同的目录.这样避免了同时复制多个大的文件造成的速度减慢:将所有任务手动操作之后,剩下 ...

  7. ubuntu 16.4 安装配置IK6.3.2

    1. 从官网下载对应的解析版本 https://github.com/medcl/elasticsearch-analysis-ik/releases/tag/v6.3.2 2. 配置环境 安装mvn ...

  8. JavaScript and Ruby in ABAP

    Netweaver里有个mini JavaScript engine CL_JAVA_SCRIPT, 对于Js code的编译和执行都是用system call完成. 只能当玩具用:report SJ ...

  9. IOS NSNotificationCenter(通知 的使用)监听文本框的文字改变

    监听文本框的文字改变 * 一个文本输入框的文字发生改变时,文本输入框会发出一个UITextFieldTextDidChangeNotification通知 * 因此通过监听通知来监听文本输入框的文字改 ...

  10. Problem K: 搜索基础之棋盘问题

    Problem K: 搜索基础之棋盘问题 Time Limit: 1 Sec  Memory Limit: 128 MBSubmit: 92  Solved: 53[Submit][Status][W ...