欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

android多行标签热点示例

程序员文章站 2022-06-04 12:18:42
复制代码 代码如下:package com.test.mytest.widget; import java.util.list; import android.cont...

复制代码 代码如下:

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;

}