android多行标签热点示例
代码如下:
package com.test.mytest.widget;
import java.util.List;
import android.content.Context;
import android.os.Handler;
import android.util.AttributeSet;
import android.widget.LinearLayout;
import android.widget.TextView;
public class MutipleLabelLayout extends LinearLayout {
public MutipleLabelLayout(Context context, AttributeSet attrs) {
super(context, attrs);
}
public MutipleLabelLayout(Context context) {
super(context);
}
public MutipleLabelLayout(Context context, List<String> list) {
super(context);
mList = list;
mContext = context;
}
@Override
protected void onAttachedToWindow() {
super.onAttachedToWindow();
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
init();
}
}, 500);
}
private void init() {
this.setOrientation(LinearLayout.VERTICAL);
LinearLayout row = new LinearLayout(mContext);
row.setOrientation(LinearLayout.HORIZONTAL);
this.addView(row);
int rowWidth = this.getMeasuredWidth();
int viewSumWidth = 0;
for (String label : mList) {
TextView labelView = new TextView(mContext);
labelView.setText(label);
labelView.setPadding(10, 0, 10, 0);
labelView.setLayoutParams(new LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
row.addView(labelView);
labelView.measure(0, 0);
viewSumWidth += labelView.getMeasuredWidth();
if (viewSumWidth > rowWidth) {
row.removeView(labelView);
row = new LinearLayout(mContext);
row.setOrientation(LinearLayout.HORIZONTAL);
this.addView(row);
row.addView(labelView);
labelView.measure(0, 0);
viewSumWidth = labelView.getMeasuredWidth();
}
// System.out.println("viewSumWidth: " + viewSumWidth);
// System.out.println("rowWidth: " + rowWidth);
}
}
private List<String> mList;
private Context mContext;
}
免责声明:
① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。
② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341