package com.test.testview;

 import java.util.ArrayList;

 import android.content.Context;
import android.graphics.Color;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView; import com.test.testview.R; public class HotwordsViewgroup extends LinearLayout implements
View.OnClickListener
{
private final static int VIEW_MARGIN_HORIZONTAL = 50;
private final static int VIEW_MARGIN_VERTICAL = 20; private HotwordsOnclickListener mHotwordsOnclickListener; public static interface HotwordsOnclickListener
{
/**
* 触发onclik回调,根据TextView.getText去做相关事情
*
* @param view
*/
public void hotwordOnclick(TextView view);
} public HotwordsViewgroup(Context context, AttributeSet attrs)
{
super(context, attrs);
init();
} public HotwordsViewgroup(Context context, AttributeSet attrs, int defStyle)
{
super(context, attrs, defStyle);
init();
} public HotwordsViewgroup(Context context)
{
super(context);
init();
} /**
* 设置每个关键词点击事件监听
*
* @param listener
*/
public void setHotwordOnclickListener(HotwordsOnclickListener listener) {
mHotwordsOnclickListener = listener;
} private void init() {
setOrientation(HORIZONTAL);
} public void setData(final ArrayList<String> list) {
removeAllViews();
if (list != null) {
for (int i = 0; i < list.size(); i++) {
String content = list.get(i);
if (TextUtils.isEmpty(content)) {
continue;
}
TextView textview = new TextView(getContext());
textview.setTextColor(Color.WHITE);
textview.setBackgroundResource(R.drawable.hot_words_bg);
textview.setTextSize(18);
textview.setOnClickListener(this);
textview.setMinimumWidth(100);
textview.setSingleLine();
textview.setEllipsize(TextUtils.TruncateAt.END);
textview.setText(content);
LinearLayout.LayoutParams layoutparams = new LinearLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
addView(textview, layoutparams);
}
}
} /**
* 重写onLayout,在layout每个元素之前,需要计算该元素是否需要折行到下一行显示
*/
protected void onLayout(boolean changed, int l, int t, int r, int b) {
int count = getChildCount();
if (count == 0) {
super.onLayout(changed, l, count, r, b);
return;
}
int lengthX = 0;
int lengthY = 0;
for (int i = 0; i < count; i++) {
View child = getChildAt(i);
int width = child.getMeasuredWidth();
int height = child.getMeasuredHeight();
child.layout(lengthX, lengthY, lengthX + width, lengthY + height);
int right = lengthX + width + VIEW_MARGIN_HORIZONTAL;
int nextWidth = 0;
if (i < count - 1) {
nextWidth = getChildAt(i + 1).getMeasuredWidth();
}
if (right + nextWidth > (r - l)) {
lengthX = 0;
lengthY += height + VIEW_MARGIN_VERTICAL;
} else {
lengthX = right;
}
}
} @Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int count = getChildCount();
if (count == 0) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
return;
}
int lengthX = 0;
int lengthY = 0;
int r = MeasureSpec.getSize(widthMeasureSpec);
// 定义子View的宽度最大不能超过r宽度
int childWidth = MeasureSpec.makeMeasureSpec(r, MeasureSpec.AT_MOST);
for (int i = 0; i < getChildCount(); i++) {
View child = getChildAt(i);
child.measure(childWidth, MeasureSpec.UNSPECIFIED);
int width = child.getMeasuredWidth();
int height = child.getMeasuredHeight();
int right = lengthX + width + VIEW_MARGIN_HORIZONTAL;
int nextWidth = 0;
// 非最后一个元素需要尝试计算一下能否放的下
if (i < count - 1) {
nextWidth = getChildAt(i + 1).getMeasuredWidth();
} else {
// 最后一个计算高度的时候要加最后一排的高度
lengthY += height;
}
if (right + nextWidth > r) {
// 如果放不下,换行
lengthX = 0;
lengthY += height + VIEW_MARGIN_VERTICAL;
} else {
// 如果能放下,往后加
lengthX = right;
}
}
setMeasuredDimension(
widthMeasureSpec,
MeasureSpec.makeMeasureSpec(lengthY,
MeasureSpec.getMode(heightMeasureSpec)));
} @Override
public void onClick(View view) {
if (mHotwordsOnclickListener != null) {
mHotwordsOnclickListener.hotwordOnclick((TextView) view);
}
}
}

Android自动折行TextView Group的更多相关文章

  1. UIlabel多行文字自动换行 (自动折行)

    UIView *footerView = [[UIView alloc]initWithFrame:CGRectMake(, , , )]; UILabel *label = [[UILabel al ...

  2. emacs自动折行设置

    - emacs自动折行     - 临时设置下 M-x `toggle-truncate-lines`    - init.el 中添加 `(toggle-truncate-lines 1)`

  3. CSS3设置内容超过一定长度后自动折行

    在用编辑器保存的数据到数据库的时候经常是在我们的内容前后加一个P标签,但是出来之后是一行,有时候会超过边框的宽度,所以研究了如何折行,如下代码: <!DOCTYPE html> <h ...

  4. Bootstrap table 元素列内容超长自动折行显示方法?

    共需要四步: 1.在table元素的父容器div加上:class="table-responsive" 3.设置表头th的width:<th width="20%& ...

  5. table中td 内容超长 自动折行 (含字母数字文字)

    <table style="width:100%;table-layout:fixed;"> //列宽由表格宽度和列宽度设定 <thead> <th& ...

  6. div 纯数字很长时无法折行解决

    <div style="width:100%;word-wrap:break-word;word-spacing:normal;"> </div> 加上红色 ...

  7. UILabel折行问题 从文件输入文本

    当内存的字符串对象中有\n时,该字符串会在UILabel展示时进行折行. 如果字符串是从plist中拿到的 1,字符串在plist中输入时打入过回车(即字符串在plist中展示也是折行的),那么字符串 ...

  8. [原] Android自动打包之命令行打包

    Android自动打包流程详细图: 总结为以下几个步骤: 1. 生成R文件 2. Java代码编译成class文件 3. class文件生成dex文件 4. 打包资源 5. 生成apk 6. 创建密匙 ...

  9. [修正] Firemonkey 中英文混排折行问题(移动平台)

    问题:FMX 在移动平台的文字显示并非由该平台的原生 API 来显示,而是由 FMX.TextLayout.GPU 来处理,也许是官方没留意到中文字符的问题,造成在中英文混排折行时,有些问题. 适用: ...

随机推荐

  1. 算法复习——扫描线(hdu1542)

    题目: Problem Description There are several ancient Greek texts that contain descriptions of the fable ...

  2. bzoj3210 花神的浇花集会 坐标

    题目大意:给定平面上的n个点,求一个点到这n个点的切比雪夫距离之和最小 与3170不同的是这次选择的点无需是n个点中的一个 首先将每个点(x,y)变为(x+y,x-y) 这样新点之间的曼哈顿距离的一半 ...

  3. 最新版浏览器报错net::ERR_INSECURE_RESPONSE原因

    访问的网址与接口请求的域名不一致,新版的chrome浏览器出于安全的考虑会将请求进行拦截,并报错net::ERR_INSECURE_RESPONSE

  4. SharePoint 2013 SSO-Secure Store Service在实际案例中的应用

    文章目录: Secure Store Service介绍 Secure Store Service部署 Secure Store Service应用 之前有一篇博客讲到使用EMSManagedAPI操 ...

  5. net1:post,get方式传值,读写cookie,读XML文件,写script语句,跳转页面,response与request类

    原文发布时间为:2008-07-29 -- 来源于本人的百度文章 [由搬家工具导入] using System;using System.Data;using System.Configuration ...

  6. JAVA特性面试题:

    1.简要介绍java程序的健壮性. 答:JAVA程序会在编译和运行的时候自动的检测可能出现的错误,而且它是一种强类型语言,对于类型的检查很严格,而且它的垃圾回收机制也有效的避免了内存的泄漏. 2.为什 ...

  7. luogu 1355 神秘大三角 判断点和三角形的位置关系 面积法 叉积法

    题目链接 题目描述 判断一个点与已知三角形的位置关系. 输入输出格式 输入格式: 前三行:每行一个坐标,表示该三角形的三个顶点 第四行:一个点的坐标,试判断该点与前三个点围成三角形的位置关系 (详见样 ...

  8. C#中IPAddress类/Dns类/IPHostEntry类/IPEndPoint用法简介

    C#中IPAddress类/Dns类/IPHostEntry类/IPEndPoint用法简介 IP是一种普遍应用于因特网.允许不同主机能够相互找到对方的寻址协议.IP地址由4个十进制的数字号码所组成, ...

  9. 无法安装64位版本的visio/office,因为在您的PC上找到了以下32位程序的解决办法

    解决方案:按下win+R键,打开运行,输入regedit,打开注册表,依次定位到 HKEY_CLASSES_ROOT\Installer\Products,展开Products后  将这些“00005 ...

  10. es6 Number.isFinite()、Number.isNaN()、Number.isInteger()、Math.trunc()、Math.sign()、Math.cbrt()、Math.fround()、Math.hypot()、Math 对数方法

    ES6在Number对象上,新提供了Number.isFinite()和Number.isNaN()两个方法,用来检查Infinite和NaN这两个特殊值. Number.isFinite()用来检查 ...