自定义控件名称
public class CheyouquanLinearLayout extends ViewGroup {
int mLeft, mRight, mTop, mBottom;
@SuppressWarnings("rawtypes")
Hashtable map = new Hashtable();
public CheyouquanLinearLayout(Context context) {
super(context);
}
public CheyouquanLinearLayout(Context context, int horizontalSpacing, int verticalSpacing) {
super(context);
}
public CheyouquanLinearLayout(Context context, AttributeSet attrs) {
super(context, attrs);
}
@SuppressLint("DrawAllocation")
@SuppressWarnings("unchecked")
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int mWidth = MeasureSpec.getSize(widthMeasureSpec);
int mCount = getChildCount();
int mX = 0;
int mY = 0;
mLeft = 0;
mRight = 0;
mTop = 0;
mBottom = 0;
int j = 0;
@SuppressWarnings("unused")
View lastview = null;
for (int i = 0; i < mCount; i++) {
final TextView child = (TextView) getChildAt(i);
String ss=child.getText().toString();
child.measure(MeasureSpec.UNSPECIFIED, MeasureSpec.UNSPECIFIED);
// 此处增加onlayout中的换行判断,用于计算所需的高度
int childw = child.getMeasuredWidth();
int childh = child.getMeasuredHeight();
mX += childw; //将每次子控件宽度进行统计叠加,如果大于设定的高度则需要换行,高度即Top坐标也需重新设置
Position position = new Position();
mLeft = getPosition(i - j, i);
mRight = mLeft + childw;
if (mX >= mWidth) {
mX = childw;
mY += childh;
j = i;
mLeft = 0;
mRight = mLeft + child.getMeasuredWidth();
mTop = mY;
//PS:如果发现高度还是有问题就得自己再细调了
}
mBottom = mTop + child.getMeasuredHeight();
mY = mTop; //每次的高度必须记录 否则控件会叠加到一起
position.left = mLeft;
position.top = mTop;
position.right = mRight;
position.bottom = mBottom;
map.put(child, position);
}
setMeasuredDimension(mWidth, mBottom);
}
@Override
protected LayoutParams generateDefaultLayoutParams() {
return new LayoutParams(1, 1); // default of 1px spacing
}
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
int count = getChildCount();
for (int i = 0; i < count; i++) {
View child = getChildAt(i);
Position pos = (Position) map.get(child);
if (pos != null) {
child.layout(pos.left, pos.top, pos.right, pos.bottom);
} else {
Log.i("MyLayout", "error");
}
}
}
private class Position {
int left, top, right, bottom;
}
public int getPosition(int IndexInRow, int childIndex) {
if (IndexInRow > 0) {
return getPosition(IndexInRow - 1, childIndex - 1)
+ getChildAt(childIndex - 1).getMeasuredWidth();
}
return getPaddingLeft();
}
动态加载人名
// 赞的人名
DisplayMetrics metric = new DisplayMetrics();
context.getWindowManager().getDefaultDisplay().getMetrics(metric);
//dp转化成px
float scale = context.getResources().getDisplayMetrics().density;
int widthQi=(int) (125*scale + 0.5f);
int widthQuan = metric.widthPixels-widthQi; //获取屏幕的宽度
float widthQ=0;
for (int a = 0; a < zList.size(); a++) {
String name=zList.get(a).getNick_name();
final String userid=zList.get(a).getUser_id();
if(a!=0){
TextView tv_dun=new TextView(context);
tv_dun.setText(",");
tv_dun.setTextColor(Color.rgb(20, 150, 193));
viewHolder.tv_zan_name.addView(tv_dun);
int wName2 = View.MeasureSpec.makeMeasureSpec(0,View.MeasureSpec.UNSPECIFIED);
int hName2 = View.MeasureSpec.makeMeasureSpec(0,View.MeasureSpec.UNSPECIFIED);
tv_dun.measure(wName2,hName2);
widthQ=tv_dun.getMeasuredWidth()+widthQ;
}
/***************以下为换行操作start**********************/
//获取数据宽度
TextView tvName=new TextView(context);
tvName.setText(name);
tvName.setTextSize(14);
int wgName = View.MeasureSpec.makeMeasureSpec(0,View.MeasureSpec.UNSPECIFIED);
int hgName = View.MeasureSpec.makeMeasureSpec(0,View.MeasureSpec.UNSPECIFIED);
tvName.measure(wgName, hgName);
//获取字符串的长度
int widthName=tvName.getMeasuredWidth();//px
//获取前面的本行的所有宽度
float widthD=widthQ;
widthQ=widthName+widthQ;
if(widthQ<=widthQuan){
TextView tv=new TextView(context);
tv.setText(name);
tv.setTextSize(14);
tv.setTextColor(Color.rgb(20, 150, 193));
viewHolder.tv_zan_name.addView(tv);
tv.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(context, MyPhotoActivity.class);
intent.putExtra("friends_id",userid);
context.startActivity(intent);
}
});
}else{
float widths=widthQuan-widthD;
String b="";
int wss=0;
for(int h=0;h<name.length();h++){
b=b+name.charAt(h);
TextView tvName2=new TextView(context);
tvName2.setText(b);
tvName2.setTextSize(14);
int wName2 = View.MeasureSpec.makeMeasureSpec(0,View.MeasureSpec.UNSPECIFIED);
int hName2 = View.MeasureSpec.makeMeasureSpec(0,View.MeasureSpec.UNSPECIFIED);
tvName2.measure(wName2,hName2);
//获取字符串的长度
int wsName2=tvName2.getMeasuredWidth();//px
if(wsName2>widths){
wss=h-1;
break;
}else if(wsName2==widths){
wss=h;
break;
}
}
/**一个textview拆成两个textview*/
String name1=name.substring(0, wss+1);
String name2=name.substring(wss+1,name.length());
if(!TextUtils.isEmpty(name1)){
TextView tv=new TextView(context);
tv.setText(name1);
tv.setTextSize(14);
tv.setTextColor(Color.rgb(20, 150, 193));
viewHolder.tv_zan_name.addView(tv);
tv.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(context, MyPhotoActivity.class);
intent.putExtra("friends_id",userid);
context.startActivity(intent);
}
});
}
if(!TextUtils.isEmpty(name2)){
TextView tv=new TextView(context);
tv.setText(name2);
tv.setTextSize(14);
tv.setEllipsize(TruncateAt.END);
tv.setTextColor(Color.rgb(20, 150, 193));
viewHolder.tv_zan_name.addView(tv);
tv.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(context, MyPhotoActivity.class);
intent.putExtra("friends_id",userid);
context.startActivity(intent); }
});
}
TextView tv6=new TextView(context);
tv6.setText(name2);
tv6.setTextSize(14);
int wg6 = View.MeasureSpec.makeMeasureSpec(0,View.MeasureSpec.UNSPECIFIED);
int hg6 = View.MeasureSpec.makeMeasureSpec(0,View.MeasureSpec.UNSPECIFIED);
tv6.measure(wg6, hg6);
//获取字符串的长度
int w2Px=tv6.getMeasuredWidth();//px
widthQ=w2Px;
/*************以上为换行操作end*******************/
}
}