Android自定义View之组合控件实现类似电商app顶部栏
作者:Brioal 发布时间:2023-07-26 22:14:10
标签:Android,自定义,组合控件,电商app,顶部栏
本文实例为大家分享了Android自定义View之组合控件,仿电商app顶部栏的相关代码,供大家参考,具体内容如下
效果图:
分析:左右两边可以是TextView和Button,设置drawableTop即可,中间的看着像是EditText,但是用过淘宝天猫等类似app的话会发现点击搜索不是在当前Activit进行搜索的,是跳转到另外的页面进行的,所以用TextView然后设置背景即可. 实现流程
参数列表:
设置属性文件:values下建立attrs.xml文件,添加需要自定义的属性.
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="TopBar">
<attr name="left_text" format="string" />// 左边文字
<attr name="right_text" format="string" /> // 右边文字
<attr name="center_text" format="string" />// 中间文字
<attr name="side_text_size" format="dimension" />//边界按钮大小
<attr name="center_text_size" format="dimension" />//中间文字大小
<attr name="text_color" format="color" />//文字颜色
<attr name="back_color" format="color" />//背景颜色
<attr name="left_icon" format="reference" />//左边的icon
<attr name="right_icon" format="reference" />//右边的icon
<attr name="center_icon" format="reference" />//中间的icon
</declare-styleable>
</resources>
代码中获取布局文件中设置的属性:
TypedArray array = getContext().obtainStyledAttributes(attrs, R.styleable.TopBar);
mLeftText = array.getString(R.styleable.TopBar_left_text);
mRightText = array.getString(R.styleable.TopBar_right_text);
mCenterText = array.getString(R.styleable.TopBar_center_text);
side_text_size = (int) array.getDimension(R.styleable.TopBar_side_text_size, 10);
center_text_size = (int) array.getDimension(R.styleable.TopBar_center_text_size, 15);
mLeft_icon = array.getDrawable(R.styleable.TopBar_left_icon);
mRight_icon = array.getDrawable(R.styleable.TopBar_right_icon);
mCenter_icon = array.getDrawable(R.styleable.TopBar_center_icon);
text_color = array.getColor(R.styleable.TopBar_text_color, getResources().getColor(R.color.colorAccent));
back_color = array.getColor(R.styleable.TopBar_back_color, getResources().getColor(R.color.colorPrimary));
array.recycle();
设置背景颜色:
setBackgroundColor(back_color);
添加按钮:
//设置内容
mLeftButton = new Button(getContext());//创建按钮
mLeftButton.setText(mLeftText);//设置文字
mLeftButton.setTextSize(side_text_size);//设置文字大小
mLeftButton.setTextColor(text_color);//设置文字颜色
mLeftButton.setBackgroundColor(Color.TRANSPARENT);//设置按钮的背景为透明
LayoutParams leftParams = new LayoutParams(80, 150);//设置布局
mLeft_icon.setBounds(0, 0, 55, 55); //设置icon的大小
mLeftButton.setCompoundDrawables(null, mLeft_icon, null, null); //添加icon
mLeftButton.setGravity(Gravity.CENTER);//设置置中
addView(mLeftButton, leftParams);//添加按钮
//右按钮类似,就不加注释了
mRightButton = new Button(getContext());
mRightButton.setText(mRightText);
mRightButton.setTextSize(side_text_size);
mRightButton.setTextColor(text_color);
mRightButton.setBackgroundColor(Color.TRANSPARENT);
mRight_icon.setBounds(0, 0, 55, 55);
LayoutParams rightParams = new LayoutParams(80, 150);
mRightButton.setCompoundDrawables(null, mRight_icon, null, null);
mRightButton.setGravity(Gravity.CENTER);
addView(mRightButton, rightParams);
添加中间的TextView:(布局的排列是按添加顺序的,所以中间TextVIew的添加应该是在两个按钮中间的):
mCenterTextView = new TextView(getContext());//初始化TextView
mCenterTextView.setText(mCenterText);//设置文字
mCenterTextView.setTextSize(center_text_size);//设置文字大小
mCenterTextView.setTextColor(text_color);//设置文字颜色
mCenterTextView.setGravity(Gravity.CENTER_VERTICAL | Gravity.LEFT);//设置文字靠左
mCenter_icon.setBounds(0, 0, 50, 50);//设置icon大小
mCenterTextView.setCompoundDrawables(mCenter_icon, null, null, null);//添加icon
LayoutParams params = new LayoutParams(0, 70);//创建布局属性 mCenterTextView.setBackground(getResources().getDrawable(R.drawable.bg_search));//设置背景
params.weight = 1;//设置权重
params.gravity = Gravity.CENTER;//设置居中
params.setMargins(10, 0, 10, 0);//设置边界
addView(mCenterTextView, params);//添加
处理高度的wrap_content属性:
重写onMeasure属性,对wrap_content设置一个指定值
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
int specWidth = MeasureSpec.getSize(widthMeasureSpec);//获取宽度
int specHeight = MeasureSpec.getSize(heightMeasureSpec);//获取高度
int heightMode = MeasureSpec.getMode(heightMeasureSpec);//获取高度的测量模式
int height = 0;//初始化要设置的高度
if (heightMode == MeasureSpec.EXACTLY) {//如果是确定的值,包括match_parent
height = specHeight; //最终的值即为测量值
} else {
height = 120; //如果不是确定的值就设置为指定的高度
if (heightMode == MeasureSpec.AT_MOST) {//如果是wrap_content就取测量值和指定值得最小值作为最终的值
height = Math.min(specHeight, 120);
}
}
setMeasuredDimension(specWidth, height);//设置宽高属性
}
添加点击事件:
需要自定义一个回调
public interface onClick {
void onLeftButtonClick();
void onCenterButtonClick();
void onRightButtonClick();
}
创建一个回调并创建setX方法
private onClick onClick;
public void setOnClick(TopBar.onClick onClick) {
this.onClick = onClick;
}
在添加按钮的时候添加点击事件
mLeftButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if (onClick != null) {
onClick.onLeftButtonClick();
}
}
});
mCenterTextView.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if (onClick != null) {
onClick.onCenterButtonClick();
}
}
});
mRightButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if (onClick != null) {
onClick.onRightButtonClick();
}
}
});
至此自定义的组合控件就完成了,下面贴一下使用的方法:
布局文件:
<com.brioa.diyviewtest.view.TopBar xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/topBar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
app:center_icon="@mipmap/ic_search"
app:center_text="输入关键字检索"
app:center_text_size="10sp"
app:left_icon="@mipmap/ic_scan"
app:left_text="扫一扫"
app:right_icon="@mipmap/ic_msg"
app:right_text="消息"
app:side_text_size="6sp"
app:text_color="#ffff">
</com.brioa.diyviewtest.view.TopBar>
代码设置:
mTopBar = (TopBar) findViewById(R.id.topBar);
mTopBar.setOnClick(new TopBar.onClick() {
@Override
public void onLeftButtonClick() {
Toast.makeText(mContext, "LeftClick", Toast.LENGTH_SHORT).show();
}
@Override
public void onCenterButtonClick() {
Toast.makeText(mContext, "CenterClick", Toast.LENGTH_SHORT).show();
}
@Override
public void onRightButtonClick() {
Toast.makeText(mContext, "RightClick", Toast.LENGTH_SHORT).show();
}
});
最终效果:


猜你喜欢
- 好久没有写过博客了,最近因项目需求,需要用到Socket来进行通信,简单写了几个例子,记录一下,代码很简单,无非就是接收与发送,以及接收到数
- 一、基于配置的异常处理SpringMVC 提供了一个处理控制器方法执行过程中所出现的异常的接口:HandlerExceptionResolv
- 前言JDK 1.5 之前 synchronized 的性能是比较低的,但在 JDK 1.5 中,官方推出一个重量级功能 Lock,一举改变了
- 前言之前看到某公司的官网的文章的浏览量刷新一次网页就会增加一次,给人的感觉不太好,一个公司的官网给人如此直白的漏洞,我批量发起请求的时候发现
- 在实际项目的开发过程中,所涉及的EXCEL往往会比较复杂,并且列中还会带有一些计算公式,这就给读取带来了很大的困难,曾经尝试过一些免费的第三
- 一、定义实体类Person,封装生成的数据package net.dc.test;public class Person { private
- Java操作redis设置第二天凌晨过期场景在做查询数据的时候,遇到了需要设置数据在redis中第二天过期的问题,但是redis又没有对应的
- 一、简述首先,Java 8引入了java.time.LocalDate来表示一个没有时间的日期。其次,使用Java 8版本,还需要更新jav
- 前言小伙伴们知道,在 Shiro 中,默认是支持权限通配符的,例如系统用户有如下一些权限:system:user:addsystem:use
- 前言现如今几乎大多数Java应用,例如我们耳熟能详的tomcat, struts2, netty...等等数都数不过来的软件,要满足通用性,
- 本文实例为大家分享了Unity苹果手机Taptic震动的具体代码,供大家参考,具体内容如下文件:ios震动.zip将上方文件解压之后将Mul
- 分页是Java Web项目常用的功能,昨天在Spring MVC中实现了简单的分页操作和搜索分页,在此记录一下。使用的框架为(MyBatis
- 一、环境准备准备开发环境创建一个Maven项目pom.xml添加依赖resources下添加spring的配置文件applicationCo
- 这是进行Java Web开发必备的一个过程,仅供新手参考,高手可以忽略!JDK 和 JRE 的区别JRE(Java Runtime Envi
- 本文实例为大家分享了C++实现简单酒店管理系统的具体代码,供大家参考,具体内容如下酒店管理系统设计报告一、 需求分析题目要求如下:某酒店有客
- 1. 配置 * 具体步骤:编写一自定义 * 类实现接口 HandlerInterceptorHandlerInterceptor 接口: 可
- 本文主要描述在C#中线程同步的方法。线程的基本概念网上资料也很多就不再赘述了。直接接入 主题,在多线程开发的应用中,线程同步是不可避免的。在
- Ben Evans是一名资深培训师兼顾问,他在演讲可视化垃圾回收中从基础谈起讨论了垃圾回收。以下是对其演讲的简短总结。基础当谈到释放不再使用
- 本文列举了几个方法: 1. 使用java.math.BigDecimal &n
- 1、定义一个接口 Animalpackage com.zh.vo;public interface Animal { void