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

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

cfb16bb787d5dbd1dceb6fc2bff54891.png

21968e7f89e7ed61b265bca263ba3a42.png

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

0a7e5b5c85891e872923c13564cbed01.png

667c08d7162c24dd6fe9198be2ec7ce8.png

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模仿微信收藏文件的标签处理功能,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对脚本之家网站的支持!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值