欢迎来到代码驿站!

Android代码

当前位置:首页 > 移动开发 > Android代码

Android模仿微信收藏文件的标签处理功能

时间:2021-06-24 08:18:49|栏目:Android代码|点击:

 最近需要用到微信的标签功能(如下图所示)。该功能可以添加已有标签,也可以自定义标签。也可以删除已编辑菜单。研究了一番。发现还是挺有意思的,模拟实现相关功能。

该功能使用类似FlowLayout的功能。Flowlayout为一个开源软件(https://github.com/ApmeM/android-flowlayout ),功能为自动换行的布局类型

import android.content.Context; 
import android.util.AttributeSet; 
import android.view.View; 
import android.view.ViewGroup; 
/** 
* 
* @author RAW 
*/ 
public class FlowLayout extends ViewGroup { 
private final static int PAD_H = 2, PAD_V = 2; // Space between child views. 
private int mHeight; 
public FlowLayout(Context context) { 
super(context); 
} 
public FlowLayout(Context context, AttributeSet attrs) { 
super(context, attrs); 
} 
@Override 
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { 
assert (MeasureSpec.getMode(widthMeasureSpec) != MeasureSpec.UNSPECIFIED); 
final int width = MeasureSpec.getSize(widthMeasureSpec) - getPaddingLeft() - getPaddingRight(); 
int height = MeasureSpec.getSize(heightMeasureSpec) - getPaddingTop() - getPaddingBottom(); 
final int count = getChildCount(); 
int xpos = getPaddingLeft(); 
int ypos = getPaddingTop(); 
int childHeightMeasureSpec; 
if(MeasureSpec.getMode(heightMeasureSpec) == MeasureSpec.AT_MOST) 
childHeightMeasureSpec = MeasureSpec.makeMeasureSpec(height, MeasureSpec.AT_MOST); 
else 
childHeightMeasureSpec = MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED); 
mHeight = 0; 
for(int i = 0; i < count; i++) { 
final View child = getChildAt(i); 
if(child.getVisibility() != GONE) { 
child.measure(MeasureSpec.makeMeasureSpec(width, MeasureSpec.AT_MOST), childHeightMeasureSpec); 
final int childw = child.getMeasuredWidth(); 
mHeight = Math.max(mHeight, child.getMeasuredHeight() + PAD_V); 
if(xpos + childw > width) { 
xpos = getPaddingLeft(); 
ypos += mHeight; 
} 
xpos += childw + PAD_H; 
} 
} 
if(MeasureSpec.getMode(heightMeasureSpec) == MeasureSpec.UNSPECIFIED) { 
height = ypos + mHeight; 
} else if(MeasureSpec.getMode(heightMeasureSpec) == MeasureSpec.AT_MOST) { 
if(ypos + mHeight < height) { 
height = ypos + mHeight; 
} 
} 
height += 5; // Fudge to avoid clipping bottom of last row. 
setMeasuredDimension(width, height); 
} // end onMeasure() 
@Override 
protected void onLayout(boolean changed, int l, int t, int r, int b) { 
final int width = r - l; 
int xpos = getPaddingLeft(); 
int ypos = getPaddingTop(); 
for(int i = 0; i < getChildCount(); i++) { 
final View child = getChildAt(i); 
if(child.getVisibility() != GONE) { 
final int childw = child.getMeasuredWidth(); 
final int childh = child.getMeasuredHeight(); 
if(xpos + childw > width) { 
xpos = getPaddingLeft(); 
ypos += mHeight; 
} 
child.layout(xpos, ypos, xpos + childw, ypos + childh); 
xpos += childw + PAD_H; 
} 
} 
} // end onLayout() 
}

点击下载源码

上一篇:android通过代码的形式来实现应用程序的方法

栏    目:Android代码

下一篇:Android 模拟器的使用详细介绍

本文标题:Android模仿微信收藏文件的标签处理功能

本文地址:http://www.codeinn.net/misctech/147229.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有