首页 > 安全资讯 >

美观的自定义选择标签FlowLayout外加支持左中右对齐及滚动等功能_图文加源码

16-12-05

现在互联网处于一个比较个性化的阶段,意思就是要根据用户的个人兴趣和口味来筛选我们的数据并进行呈现,比如一些新闻类的app,怎么知道用户比较喜欢什么?现在比较流行的一种方式就是给他一些关键字,让她选,再进行针对性的给予内容

现在互联网处于一个比较个性化的阶段,意思就是要根据用户的个人兴趣和口味来筛选我们的数据并进行呈现,比如一些新闻类的app,怎么知道用户比较喜欢什么?现在比较流行的一种方式就是给他一些关键字,让她选,再进行针对性的给予内容,我于是就搞了一个比较漂亮的安卓选择关键字标签的demo分享给大家。

 

漂亮吧,呵呵!

我们现在来学习一下它是怎么弄成的?

1. 他整体的一部分是使用到的一个自定义的PopupWindow控件,作用主要是在页面上盖一层半透明的层,然后可以屏蔽底下层的操作事件,为上面的内容提供一个层平台,代码如下:

public class SelectLabelPopupWindow extends PopupWindow implements View.OnClickListener,TagListView.OnTagCheckedChangedListener,TagListView.OnTagClickListener{
final String TAG = "SelectLabelPopupWindow";
private View mContentView;
private TagListView tagview;
private Context mContext;
private List selectedTagList=new ArrayList<>();
private OnClickConfirmListener mOnClickConfirmListener;
public SelectLabelPopupWindow(Context context) {
super(context);
mContext=context;
LayoutInflater inflater = (LayoutInflater) mContext
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
mContentView = inflater.inflate(R.layout.pop_win_select_label, null);
mContentView.findViewById(R.id.delView).setOnClickListener(this);
mContentView.findViewById(R.id.confirmBtn).setOnClickListener(this);
tagview=(TagListView)mContentView.findViewById(R.id.tagview);
tagview.setTagViewTextColorRes(android.R.color.white);
int halfheight=(int)(mContext.getApplicationContext().getResources().getDisplayMetrics().heightPixels*0.5);
ScrollView tagScorllview=(ScrollView)mContentView.findViewById(R.id.tagScorllview);
ViewGroup.LayoutParams params=tagScorllview.getLayoutParams();
params.height= halfheight;
if(BuildConfig.DEBUG){
Log.e(TAG,"SelectLabelPopupWindow() "+halfheight);
}
tagview.setOnTagCheckedChangedListener(this);
tagview.setOnTagClickListener(this);
tagview.setGravity(Gravity.LEFT);
// tagview.setDeleteMode(true);
//设置SelectPicPopupWindow的View
this.setContentView(mContentView);
//设置SelectPicPopupWindow弹出窗体的宽
this.setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
//设置SelectPicPopupWindow弹出窗体的高
this.setHeight(ViewGroup.LayoutParams.MATCH_PARENT);
//设置SelectPicPopupWindow弹出窗体可点击
this.setFocusable(true);
//设置SelectPicPopupWindow弹出窗体动画效果
this.setAnimationStyle(R.style.AnimBottom);
//实例化一个ColorDrawable颜色为半透明
ColorDrawable dw = new ColorDrawable(0xb0000000);
//设置SelectPicPopupWindow弹出窗体的背景
this.setBackgroundDrawable(dw);
}
//设置界面上要显示的标签内容列表
public void setLabelList(List aList){
for(LabelBean bean:aList){
Tag tag=new Tag();
tag.setId(bean.getId());
tag.setTitle(bean.getName());
tag.setBackgroundResId(R.drawable.tag_item_bg);
tagview.addTag(tag,true);
}
}

//当点击确定按钮时,触发的回调事件
public void setOnClickConfirmListener(OnClickConfirmListener onClickConfirmListener) {
mOnClickConfirmListener = onClickConfirmListener;
}
//当标签被点击后回调事件的处理,这里支持两种方式1.选择模式,2.删除模式,这个可以通过TagListView的setDeleteMode()设置
@Override
public void onTagCheckedChanged(TagView tagView, Tag tag) {
if(tagview.isDeleteMode()){
tagview.removeTag(tag);
}else{
if(tagView.isChecked()){
tagView.setTextColor(mContext.getResources().getColor(R.color.colorPrimary));
selectedTagList.add(tag);
}else{
tagView.setTextColor(mContext.getResources().getColor(android.R.color.white));
selectedTagList.remove(tag);
}
}

if(BuildConfig.DEBUG){
Log.e(TAG, "onTagCheckedChanged() tag "+tag.isChecked()+",view "+tagView.isChecked()+",selected size:"+selectedTagList.size());
}
}

@Override
public void onTagClick(TagView tagView, Tag tag) {
if(BuildConfig.DEBUG){
Log.e(TAG, "onTagClick() tag "+tag.isChecked()+",view "+tagView.isChecked());
}
}

//得到选中的标签名,一般用于传给后台
public String getSelectedLabelIds(){
if(null==selectedTagList || selectedTagList.isEmpty())return "";
String result="";
for(Tag bean:selectedTagList){
result=bean.getId()+","+result;
}
if(!TextUtils.isEmpty(result) && result.endsWith(",")){
result=result.substring(0,result.length()-1);
}
return result;
}

//得到选中的标签名,一般用于显示在界面,让用户知道自己之前选了那些标签
public String getSelectedLabelName(){
String names="";
for(Tag bean:selectedTagList){
names=bean.getTitle()+","+names;
}
if(!TextUtils.isEmpty(names) && names.endsWith(",")){
names=names.substring(0,names.length()-1);
}
return names;
}

public interface OnClickConfirmListener {
void onClick(SelectLabelPopupWindow popupWindow,String aIds);
}

@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.delView:
//销毁弹出框
dismiss();
break;
case R.id.confirmBtn:
if(null!=mOnClickConfirmListener){
mOnClickConfirmListener.onClick(this,getSelectedLabelIds());
}
break;
}
}
}

 

2.界面上的那个标签内容块是使用TagListView类实现的,它继承于自定义FlowLayout,FlowLayout管理了标签并可以进行左中右对齐,代码如下:

TagListView.java

public class TagListView extends FlowLayout implements OnClickListener {
private boolean mIsDeleteMode;
private OnTagCheckedChangedListener mOnTagCheckedChangedListener;
private OnTagClickListener mOnTagClickListener;
private int mTagViewBackgroundResId;
private int mTagViewTextColorResId;
private final List mTags = new ArrayList();

/**
* @param context
*/
public TagListView(Context context) {
super(context);
// TODO Auto-generated constructor stub
init();
}
/**
* @param context
* @param attributeSet
*/
public TagListView(Context context, AttributeSet attributeSet) {
super(context, attributeSet);
// TODO Auto-generated constructor stub
init();
}
/**
* @param context
* @param attributeSet
* @param defStyle
*/
public TagListView(Context context, AttributeSet attributeSet, int defStyle) {
super(context, attributeSet, defStyle);
// TODO Auto-generated constructor stub
init();
}
@Override
public void onClick(View v) {
if ((v instanceof TagView)) {
Tag localTag = (Tag) v.getTag();
if (this.mOnTagClickListener != null) {
this.mOnTagClickListener.onTagClick((TagView) v, localTag);
}
}
}

private void init() {
}

private void inflateTagView(final Tag t, boolean b) {
TagView localTagView = (TagView) View.inflate(getContext(),
R.layout.tag, null);
localTagView.setText(t.getTitle());
localTagView.setTag(t);
if (mTagViewTextColorResId <= 0) {
int c = getResources().getColor(R.color.colorPrimary);
localTagView.setTextColor(c);
}else{
localTagView.setTextColor(getResources().getColor(mTagViewTextColorResId));
}


if (mTagViewBackgroundResId <= 0) {
mTagViewBackgroundResId = R.drawable.tag_bg;
localTagView.setBackgroundResource(mTagViewBackgroundResId);
}

localTagView.setChecked(t.isChecked());
localTagView.setCheckEnable(b);
if (mIsDeleteMode) {
int k = (int) TypedValue.applyDimension(1, 5.0F, getContext()
.getResources().getDisplayMetrics());
localTagView.setPadding(localTagView.getPaddingLeft(),
localTagView.getPaddingTop(), k,
localTagView.getPaddingBottom());
localTagView.setCompoundDrawablesWithIntrinsicBounds(0, 0,
R.drawable.cancel_btn, 0);
}
if (t.getBackgroundResId() > 0) {
localTagView.setBackgroundResource(t.getBackgroundResId());
}
if ((t.getLeftDrawableResId() > 0) || (t.getRightDrawableResId() > 0)) {
localTagView.setCompoundDrawablesWithIntrinsicBounds(
t.getLeftDrawableResId(), 0, t.getRightDrawableResId(), 0);
}
localTagView.setOnClickListener(this);
localTagView
.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
public void onCheckedChanged(
CompoundButton paramAnonymousCompoundButton,
boolean paramAnonymousBoolean) {
t.setChecked(paramAnonymousBoolean);
if (TagListView.this.mOnTagCheckedChangedListener != null) {
TagListView.this.mOnTagCheckedChangedListener
.onTagCheckedChanged(
(TagView) paramAnonymousCompoundButton,
t);
}
}
});
addView(localTagView);
}
public void addTag(int i, String s) {
addTag(i, s, false);
}
public void addTag(int i, String s, boolean b) {
addTag(new Tag(i, s), b);
}
public void addTag(Tag tag) {
addTag(tag, false);
}
public void addTag(Tag tag, boolean b) {
mTags.add(tag);
inflateTagView(tag, b);
}

public void addTags(List lists) {
addTags(lists, false);
}

public void addTags(List lists, boolean b) {
for (int i = 0; i < lists.size(); i++) {
addTag((Tag) lists.get(i), b);
}
}

public List getTags() {
return mTags;
}

public View getViewByTag(Tag tag) {
return findViewWithTag(tag);
}

public void removeTag(Tag tag) {
mTags.remove(tag);
removeView(getViewByTag(tag));
}

public void setDeleteMode(boolean b) {
mIsDeleteMode = b;
}

public boolean isDeleteMode() {
return mIsDeleteMode;
}

public void setOnTagCheckedChangedListener(
OnTagCheckedChangedListener onTagCheckedChangedListener) {
mOnTagCheckedChangedListener = onTagCheckedChangedListener;
}

public void setOnTagClickListener(OnTagClickListener onTagClickListener) {
mOnTagClickListener = onTagClickListener;
}
public void setTagViewBackgroundRes(int res) {
mTagViewBackgroundResId = res;
}
public void setTagViewTextColorRes(int res) {
mTagViewTextColorResId = res;
}

public void setTags(Listlists) {
setTags(lists, false);
}

public void setTags(Listlists, boolean b) {
removeAllViews();
mTags.clear();
for (int i = 0; i < lists.size(); i++) {
addTag((Tag) lists.get(i), b);
}
}
public static abstract interface OnTagCheckedChangedListener {
public abstract void onTagCheckedChanged(TagView tagView, Tag tag);
}
public static abstract interface OnTagClickListener {
public abstract void onTagClick(TagView tagView, Tag tag);
}
}

 

FlowLayout.java

public class FlowLayout extends ViewGroup {
final String TAG = "FlowLayout";
public static final int HORIZONTAL = 0;
public static final int VERTICAL = 1;
private int horizontalSpacing = 0;
private int verticalSpacing = 0;
private int orientation = 0;
private boolean debugDraw = false;
private int mGravity = (isIcs() ? Gravity.START : Gravity.LEFT) | Gravity.TOP;
private ArrayList tempLineList=new ArrayList<>();

public FlowLayout(Context context) {
super(context);

this.readStyleParameters(context, null);
}

public FlowLayout(Context context, AttributeSet attributeSet) {
super(context, attributeSet);

this.readStyleParameters(context, attributeSet);
}

public FlowLayout(Context context, AttributeSet attributeSet, int defStyle) {
super(context, attributeSet, defStyle);

this.readStyleParameters(context, attributeSet);
}

private static boolean isIcs() {
return Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH;
}

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int sizeWidth = MeasureSpec.getSize(widthMeasureSpec) - this.getPaddingRight() - this.getPaddingLeft();
int sizeHeight = MeasureSpec.getSize(heightMeasureSpec) - this.getPaddingTop() - this.getPaddingBottom();
int modeWidth = MeasureSpec.getMode(widthMeasureSpec);
int modeHeight = MeasureSpec.getMode(heightMeasureSpec);
int size;
int mode;
if (orientation == HORIZONTAL) {
size = sizeWidth;
mode = modeWidth;
} else {
size = sizeHeight;
mode = modeHeight;
}
int lineThicknessWithSpacing = 0;
int lineThickness = 0;
int lineLengthWithSpacing = 0;
int lineLength;
int prevLinePosition = 0;
int controlMaxLength = 0;
int controlMaxThickness = 0;
tempLineList.clear();
final int count = getChildCount();
for (int i = 0; i < count; i++) {
final View child = getChildAt(i);
if (child.getVisibility() == GONE) {
continue;
}
LayoutParams lp = (LayoutParams) child.getLayoutParams();
child.measure(
getChildMeasureSpec(widthMeasureSpec, this.getPaddingLeft()
+ this.getPaddingRight(), lp.width),
getChildMeasureSpec(heightMeasureSpec, this.getPaddingTop()
+ this.getPaddingBottom(), lp.height));
int hSpacing = this.getHorizontalSpacing(lp);
int vSpacing = this.getVerticalSpacing(lp);
int childWidth = child.getMeasuredWidth();
int childHeight = child.getMeasuredHeight();
int childLength;
int childThickness;
int spacingLength;
int spacingThickness;
if (orientation == HORIZONTAL) {
childLength = childWidth;
childThickness = childHeight;
spacingLength = hSpacing;
spacingThickness = vSpacing;
} else {
childLength = childHeight;
childThickness = childWidth;
spacingLength = vSpacing;
spacingThickness = hSpacing;
}

lineLength = lineLengthWithSpacing + childLength;
lineLengthWithSpacing = lineLength + spacingLength;
boolean newLine = lp.newLine
|| (mode != MeasureSpec.UNSPECIFIED && lineLength > size);
if (newLine) {
readjustPreLineViewOnPos(size-(lineLength-childLength-hSpacing));
prevLinePosition = prevLinePosition + lineThicknessWithSpacing;
lineThickness = childThickness;
lineLength = childLength;
lineThicknessWithSpacing = childThickness + spacingThickness;
lineLengthWithSpacing = lineLength + spacingLength;
}

tempLineList.add(child);
lineThicknessWithSpacing = Math.max(lineThicknessWithSpacing,
childThickness + spacingThickness);
lineThickness = Math.max(lineThickness, childThickness);
int posX;
int posY;
if (orientation == HORIZONTAL) {
posX = getPaddingLeft() + lineLength - childLength;
posY = getPaddingTop() + prevLinePosition;
} else {
posX = getPaddingLeft() + prevLinePosition;
posY = getPaddingTop() + lineLength - childHeight;
}
lp.setPosition(posX, posY);

if(count-1==i){
readjustPreLineViewOnPos(size-lineLength);
}
controlMaxLength = Math.max(controlMaxLength, lineLength);
controlMaxThickness = prevLinePosition + lineThickness;
}
/* need to take paddings into account */
if (orientation == HORIZONTAL) {
controlMaxLength += getPaddingLeft() + getPaddingRight();
controlMaxThickness += getPaddingBottom() + getPaddingTop();
} else {
controlMaxLength += getPaddingBottom() + getPaddingTop();
controlMaxThickness += getPaddingLeft() + getPaddingRight();
}
if (orientation == HORIZONTAL) {
this.setMeasuredDimension(
resolveSize(controlMaxLength, widthMeasureSpec),
resolveSize(controlMaxThickness, heightMeasureSpec));
} else {
this.setMeasuredDimension(
resolveSize(controlMaxThickness, widthMeasureSpec),
resolveSize(controlMaxLength, heightMeasureSpec));
}
}

@TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH)
public void setGravity(int gravity) {
if (mGravity != gravity) {
if ((gravity & Gravity.RELATIVE_HORIZONTAL_GRAVITY_MASK) == 0) {
gravity |= isIcs() ? Gravity.START : Gravity.LEFT;
}
if ((gravity & Gravity.VERTICAL_GRAVITY_MASK) == 0) {
gravity |= Gravity.TOP;
}
mGravity = gravity;
requestLayout();
}
}

private int getVerticalSpacing(LayoutParams lp) {
int vSpacing;
if (lp.verticalSpacingSpecified()) {
vSpacing = lp.verticalSpacing;
} else {
vSpacing = this.verticalSpacing;
}
return vSpacing;
}

private int getHorizontalSpacing(LayoutParams lp) {
int hSpacing;
if (lp.horizontalSpacingSpecified()) {
hSpacing = lp.horizontalSpacing;
} else {
hSpacing = this.horizontalSpacing;
}
return hSpacing;
}

private void readjustPreLineViewOnPos(int aLastLen){
if(BuildConfig.DEBUG){
Log.e(TAG,"readjustPreLineViewOnPos() lastLen:"+aLastLen);
}
if(null==tempLineList || tempLineList.isEmpty())return;
/*int mid=aLastLen/2;
for(View v:tempLineList){
LayoutParams lp = (LayoutParams) v.getLayoutParams();
if(orientation == HORIZONTAL){
lp.setPosition(lp.x+mid,lp.y);
}else{
lp.setPosition(lp.x,lp.y+mid);
}
}*/
if(orientation == HORIZONTAL){
readustHorizontalPos(aLastLen);
}else{
readustVerticalPos(aLastLen);
}
tempLineList.clear();
}

private void readustHorizontalPos(int aLastLen){
int horizontalGravityFactor;
switch ((mGravity & Gravity.HORIZONTAL_GRAVITY_MASK)) {
case Gravity.LEFT:
horizontalGravityFactor = 0;
break;
case Gravity.CENTER_HORIZONTAL:
horizontalGravityFactor = 2;
break;
case Gravity.RIGHT:
horizontalGravityFactor = 1;
break;
default:
horizontalGravityFactor = 0;
break;
}
if(horizontalGravityFactor<=0)return;
int mid=aLastLen/horizontalGravityFactor;
for(View v:tempLineList){
LayoutParams lp = (LayoutParams) v.getLayoutParams();
lp.setPosition(lp.x+mid,lp.y);

}
}

private void readustVerticalPos(int aLastLen){
int verticalGravityMargin;
switch ((mGravity & Gravity.HORIZONTAL_GRAVITY_MASK)) {
case Gravity.TOP:
verticalGravityMargin = 0;
break;
case Gravity.CENTER_VERTICAL:
verticalGravityMargin = 2;
break;
case Gravity.BOTTOM:
verticalGravityMargin = 1;
break;
default:
verticalGravityMargin = 0;
break;
}
if(verticalGravityMargin<=0)return;
int mid=aLastLen/verticalGravityMargin;
for(View v:tempLineList){
LayoutParams lp = (LayoutParams) v.getLayoutParams();
lp.setPosition(lp.x,lp.y+mid);
}
}

@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
final int count = getChildCount();
for (int i = 0; i < count; i++) {
View child = getChildAt(i);
LayoutParams lp = (LayoutParams) child.getLayoutParams();
child.layout(lp.x, lp.y, lp.x + child.getMeasuredWidth(), lp.y
+ child.getMeasuredHeight());
}
}

@Override
protected boolean drawChild(Canvas canvas, View child, long drawingTime) {
boolean more = super.drawChild(canvas, child, drawingTime);
this.drawDebugInfo(canvas, child);
return more;
}

@Override
protected boolean checkLayoutParams(ViewGroup.LayoutParams p) {
return p instanceof LayoutParams;
}

@Override
protected LayoutParams generateDefaultLayoutParams() {
return new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT);
}

@Override
public LayoutParams generateLayoutParams(AttributeSet attributeSet) {
return new LayoutParams(getContext(), attributeSet);
}

@Override
protected LayoutParams generateLayoutParams(ViewGroup.LayoutParams p) {
return new LayoutParams(p);
}

private void readStyleParameters(Context context, AttributeSet attributeSet) {
TypedArray a = context.obtainStyledAttributes(attributeSet,
R.styleable.FlowLayout);
try {
horizontalSpacing = a.getDimensionPixelSize(
R.styleable.FlowLayout_horizontalSpacing, 0);
verticalSpacing = a.getDimensionPixelSize(
R.styleable.FlowLayout_verticalSpacing, 0);
orientation = a.getInteger(R.styleable.FlowLayout_orientation,
HORIZONTAL);
int defaultVal;
if(HORIZONTAL==orientation){
defaultVal=Gravity.LEFT;
}else{
defaultVal=Gravity.TOP;
}
mGravity =a.getInteger(R.styleable.FlowLayout_android_gravity,defaultVal);
debugDraw = a.getBoolean(R.styleable.FlowLayout_debugDraw, false);
} finally {
a.recycle();
}
}

private void drawDebugInfo(Canvas canvas, View child) {
if (!debugDraw) {
return;
}
Paint childPaint = this.createPaint(0xffffff00);
Paint layoutPaint = this.createPaint(0xff00ff00);
Paint newLinePaint = this.createPaint(0xffff0000);
LayoutParams lp = (LayoutParams) child.getLayoutParams();
if (lp.horizontalSpacing > 0) {
float x = child.getRight();
float y = child.getTop() + child.getHeight() / 2.0f;
canvas.drawLine(x, y, x + lp.horizontalSpacing, y, childPaint);
canvas.drawLine(x + lp.horizontalSpacing - 4.0f, y - 4.0f, x
+ lp.horizontalSpacing, y, childPaint);
canvas.drawLine(x + lp.horizontalSpacing - 4.0f, y + 4.0f, x
+ lp.horizontalSpacing, y, childPaint);
} else if (this.horizontalSpacing > 0) {
float x = child.getRight();
float y = child.getTop() + child.getHeight() / 2.0f;
canvas.drawLine(x, y, x + this.horizontalSpacing, y, layoutPaint);
canvas.drawLine(x + this.horizontalSpacing - 4.0f, y - 4.0f, x
+ this.horizontalSpacing, y, layoutPaint);
canvas.drawLine(x + this.horizontalSpacing - 4.0f, y + 4.0f, x
+ this.horizontalSpacing, y, layoutPaint);
}
if (lp.verticalSpacing > 0) {
float x = child.getLeft() + child.getWidth() / 2.0f;
float y = child.getBottom();
canvas.drawLine(x, y, x, y + lp.verticalSpacing, childPaint);
canvas.drawLine(x - 4.0f, y + lp.verticalSpacing - 4.0f, x, y
+ lp.verticalSpacing, childPaint);
canvas.drawLine(x + 4.0f, y + lp.verticalSpacing - 4.0f, x, y
+ lp.verticalSpacing, childPaint);
} else if (this.verticalSpacing > 0) {
float x = child.getLeft() + child.getWidth() / 2.0f;
float y = child.getBottom();
canvas.drawLine(x, y, x, y + this.verticalSpacing, layoutPaint);
canvas.drawLine(x - 4.0f, y + this.verticalSpacing - 4.0f, x, y
+ this.verticalSpacing, layoutPaint);
canvas.drawLine(x + 4.0f, y + this.verticalSpacing - 4.0f, x, y
+ this.verticalSpacing, layoutPaint);
}
if (lp.newLine) {
if (orientation == HORIZONTAL) {
float x = child.getLeft();
float y = child.getTop() + child.getHeight() / 2.0f;
canvas.drawLine(x, y - 6.0f, x, y + 6.0f, newLinePaint);
} else {
float x = child.getLeft() + child.getWidth() / 2.0f;
float y = child.getTop();
canvas.drawLine(x - 6.0f, y, x + 6.0f, y, newLinePaint);
}
}
}

private Paint createPaint(int color) {
Paint paint = new Paint();
paint.setAntiAlias(true);
paint.setColor(color);
paint.setStrokeWidth(2.0f);
return paint;
}

public static class LayoutParams extends ViewGroup.LayoutParams {
private static int NO_SPACING = -1;
private int x;
private int y;
private int horizontalSpacing = NO_SPACING;
private int verticalSpacing = NO_SPACING;
private boolean newLine = false;
public LayoutParams(Context context, AttributeSet attributeSet) {
super(context, attributeSet);
this.readStyleParameters(context, attributeSet);
}

public LayoutParams(int width, int height) {
super(width, height);
}

public LayoutParams(ViewGroup.LayoutParams layoutParams) {
super(layoutParams);
}

public boolean horizontalSpacingSpecified() {
return horizontalSpacing != NO_SPACING;
}

public boolean verticalSpacingSpecified() {
return verticalSpacing != NO_SPACING;
}

public void setPosition(int x, int y) {
this.x = x;
this.y = y;
}

private void readStyleParameters(Context context,
AttributeSet attributeSet) {
TypedArray a = context.obtainStyledAttributes(attributeSet,
R.styleable.FlowLayout_LayoutParams);
try {
horizontalSpacing = a
.getDimensionPixelSize(
R.styleable.FlowLayout_LayoutParams_layout_horizontalSpacing,
NO_SPACING);
verticalSpacing = a
.getDimensionPixelSize(
R.styleable.FlowLayout_LayoutParams_layout_verticalSpacing,
NO_SPACING);
newLine = a.getBoolean(
R.styleable.FlowLayout_LayoutParams_layout_newLine,
false);
} finally {
a.recycle();
}
}
}
}

 

3.标签项TagView,代码如下:

public class TagView extends ToggleButton {
private boolean mCheckEnable = true;

public TagView(Context paramContext) {
super(paramContext);
init();
}

public TagView(Context paramContext, AttributeSet paramAttributeSet) {
super(paramContext, paramAttributeSet);
init();
}

public TagView(Context paramContext, AttributeSet paramAttributeSet,
int paramInt) {
super(paramContext, paramAttributeSet, 0);
init();
}

private void init() {
setTextOn(null);
setTextOff(null);
setText("");
setBackgroundResource(R.drawable.tag_bg);
}

public void setCheckEnable(boolean paramBoolean) {
this.mCheckEnable = paramBoolean;
if (!this.mCheckEnable) {
super.setChecked(false);
}
}

public void setChecked(boolean paramBoolean) {
if (this.mCheckEnable) {
super.setChecked(paramBoolean);
}
}
}

下面介绍一下,它左中右对齐排序的设置及效果
有两个方面可以设置
1. 代码段:TagListView的setGravity()函数,现在测试的可用参数: Gravity.LEFT,Gravity. CENTER_HORIZONTAL, Gravity.RIGHT
2. Xml:在xml文件中com.tom.selectlabel.widgets.TagListView标签属性android:gravity,我测过可用的是left,center_horizontal,right
效果如下:

内容左对齐:

内容中对齐:

 

内容右对齐:

内容还可以滚动,效果如下:

点击确认后,还可以获取到他点击了什么,效果如下:

 



好的,已经写完了!

相关文章
最新文章
热点推荐