Android自定义View之组合控件实现类似电商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(); } });

最终效果:

以上就是本文的全部内容,希望对大家学习Android软件编程有所帮助,也希望大家多多支持脚本之家。

时间: 2024-09-20 10:41:34

Android自定义View之组合控件实现类似电商app顶部栏的相关文章

Android自定义View之组合控件实现类似电商app顶部栏_Android

本文实例为大家分享了Android自定义View之组合控件,仿电商app顶部栏的相关代码,供大家参考,具体内容如下 效果图: 分析:左右两边可以是TextView和Button,设置drawableTop即可,中间的看着像是EditText,但是用过淘宝天猫等类似app的话会发现点击搜索不是在当前Activit进行搜索的,是跳转到另外的页面进行的,所以用TextView然后设置背景即可. 实现流程 参数列表: 设置属性文件:values下建立attrs.xml文件,添加需要自定义的属性. <?x

Android自定义View弧线进度控件_Android

这个是一个以弧线为依托的进度控件,主要包括了两个圆弧.一个圆.一个文本.   当我们点击开始按钮的时候,会出现一个动画,逐渐的出现进度,好了,下面开始我们的编码. 新建一个类,继承自View,实现三个构造方法,接着定义变量,初始化变量的数据.代码如下: private Paint mArcPaint, mCirclePaint, mTextPaint, mPaint; private float length; private float mRadius; private float mCirc

Android自定义View圆形百分比控件(一)

做一个自定义View的小练习,效果如下 只需要画一个圆.一个圆弧.一个百分比文本,添加一个点击事件,传入百分比重绘 1.在res/values文件夹下新建attrs.xml文件,编写自定义属性: <?xml version="1.0" encoding="utf-8"?> <resources> <declare-styleable name="CirclePercentView" > <attr nam

Android自定义View弧线进度控件

这个是一个以弧线为依托的进度控件,主要包括了两个圆弧.一个圆.一个文本. 当我们点击开始按钮的时候,会出现一个动画,逐渐的出现进度,好了,下面开始我们的编码. 新建一个类,继承自View,实现三个构造方法,接着定义变量,初始化变量的数据.代码如下: private Paint mArcPaint, mCirclePaint, mTextPaint, mPaint; private float length; private float mRadius; private float mCircle

Android自定义滑动接听电话控件组实例_Android

本文根据组件开发思想,首先介绍android自定义控件,然后将自定义的控件封装为jar包.最为实现滑动接听电话控件组. 一.目录结构 二.运行效果 三.代码实现 首先,自定义一个类IncomingPhone继承RelativeLayout public IncomingPhone(Context context, AttributeSet attrs) { super(context, attrs); mContext = context; TextView textView = new Tex

Android学习自定义View(四)——继承控件(滑动时ListView的Item出现删除按钮)

MainActivity如下: package cc.testviewstudy4; import java.util.ArrayList; import java.util.HashMap; import cc.testviewstudy4.ListViewSubClass.OnDeleteListener; import android.os.Bundle; import android.widget.SimpleAdapter; import android.app.Activity; /

Android自定义下拉刷新控件RefreshableView_Android

这是在了解下拉刷新功能原理下的产物,下拉刷新可以说是国产APP里面必有的功能,连Google都为此出了SwipeRefreshLayout,一种MD风格的下拉刷新. 不过,MD风格在国内似乎很是艰难,不单单是国内系统主流仍是4.4的原因,也有用户习惯的问题,扯的有点多了,在看了许多博客之后,我突然想写一个能仿照 SwipeRefreshLayout 的兼容所有控件的下拉刷新,不单单只是 ListView,希望它也可以包容普通的View和ScrollView,经过两天的奋斗,终于搞定了,因为我的目

Android自定义ViewGroup之子控件的自动换行和添加删除

常用的布局类型并不能满足所有需求,这时就会用到ViewGroup. ViewGroup作为一个放置View的容器,并且我们在写布局xml的时候,会告诉容器(凡是以layout为开头的属性,都是为用于告诉容器的),我们的宽度(layout_width).高度(layout_height).对齐方式(layout_gravity)等:当然还有margin等:于是乎,ViewGroup需要做的事情是:给childView计算出建议的宽和高和测量模式 :决定childView的位置:为什么只是建议的宽和

Android自定义实现循环滚轮控件WheelView_Android

首先呈上Android循环滚轮效果图:   现在很多地方都用到了滚轮布局WheelView,比如在选择生日的时候,风格类似系统提供的DatePickerDialog,开源的控件也有很多,不过大部分都是根据当前项目的需求绘制的界面,因此我就自己写了一款比较符合自己项目的WheelView. 首先这个控件有以下的需求:  1.能够循环滚动,当向上或者向下滑动到临界值的时候,则循环开始滚动  2.中间的一块有一块半透明的选择区,滑动结束时,哪一块在这个选择区,就选择这快.  3.继承自View进行绘制