android多行标签 android多行标签热点示例

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

android多行标签 android多行标签热点示例

  2021-03-19 我要评论
想了解android多行标签热点示例的相关内容吗,在本文为您仔细讲解android多行标签的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点: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;

}

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们