J2ME实现展示内容自动根据屏幕宽度换行的功能
2010-11-23 20:03
645 查看
转自:http://120.132.134.205/cmdn/bbs/viewthread.php?tid=24603
由于lwuit的label中没有根据内容自动换行的设置,而项目中的TextArea的样式和Label样式的背景不同,不能使用TextArea展示内容,所以就重写了一个TextLabel的类,继承自TextArea类。实现了这个功能,其中大部分源码参考了http://j2me.cc43.com/100916/124.html中的代码。import java.util.Vector; import com.sun.lwuit.Display; import com.sun.lwuit.Font; import com.sun.lwuit.TextArea; import com.sun.lwuit.geom.Dimension; import com.sun.lwuit.plaf.Style; /** * * @ description:实现展示内容自动根据屏幕宽度换行的功能 * @ company lingxun * @ author Lancelothe * @ date 2010-9-30 */ public class TextLabel extends TextArea { private int autoWidth = 0;// 设置换行的宽度 private int maxRow = 0;// 最大行数 private boolean maxrowCode = false;// 是否设置了最大行数 private int fontCountW = 0;// 通过汉字个数计算的宽度 private final int fontFx = 14;// 字体的size,现用14个像索 private Vector rowStrings; private int widthForRowCalculations = -1; /** * 无参构造函数,默认宽度为屏幕宽度 */ public TextLabel() { this("", Display.getInstance().getDisplayWidth()); } /** * * 默认宽度为屏幕宽度 * * * @param text */ public TextLabel(String text) { this(text, Display.getInstance().getDisplayWidth()); } public TextLabel(String text, int width) { super(text); System.out.println("屏幕宽度:" + Display.getInstance().getDisplayWidth()); System.out.println("实际宽度:" + width); this.setEditable(false); // 不能编辑 this.setFocusable(false); // 不能获得焦点 this.getStyle().setBorder(null); // 没有边框 this.autoWidth = width; this.setScrollSize(new Dimension(0, 0)); // 设置无滚动条 this.getStyle().setPadding(0, 0, 4, 15); this.setPreferredW(this.autoWidth); } public TextLabel(String text, int width, int maxRow) { super(text); this.setEditable(false); // 不能编辑 this.setFocusable(false); // 不能获得焦点 this.maxRow = maxRow; if (this.maxRow > 0) this.maxrowCode = true; this.getStyle().setBorder(null); // 没有边框 this.setScrollSize(new Dimension(0, 0)); // 设置无滚动条 this.getStyle().setPadding(0, 0, 4, 15); this.autoWidth = width; this.setPreferredW(this.autoWidth); } /** * * 获取宽度 * * * @return */ public int getAutoWidth() { return autoWidth; } private Vector getRowStrings() { if (rowStrings == null || widthForRowCalculations != getWidth() - getStyle().getPadding(false, RIGHT) - getStyle().getPadding(false, LEFT)) { this.initRowString(); setShouldCalcPreferredSize(true); } return rowStrings; } /** * @ description:覆盖父类中的getLines方法 @ author Lancelothe @ parameter @ create_date * 2010-9-30 @ update_date 2010-9-30 * @see com.sun.lwuit.TextArea#getLines() */ public int getLines() { int retVal; Vector v = this.getRowStrings(); retVal = v.size(); return retVal; } /** * * 覆盖TextArea的initRowString()方法,需修改源码<br/> * */ protected void initRowString() { rowStrings = new Vector(); widthForRowCalculations = getWidth(); if (isSingleLineTextArea()) { rowStrings.addElement(getText()); return; } if (getText() == null || getText().equals("")) { return; } char[] text = preprocess(getText()); // 转化成字符数组 Style style = this.getStyle(); int textLabelWidth = this.autoWidth; // 本身的宽度 if (textLabelWidth <= 0) { textLabelWidth = Display.getInstance().getDisplayWidth(); // 默认屏幕宽度 } rowStrings = this.autoText(style.getFont(), getText(), textLabelWidth); } /** * * 获得指定宽度每行的文本数组 * * * @param font * * @param text * * @param maxWidth */ private Vector autoText(Font font, String text, int maxWidth) { Vector v = new Vector(); int fcwCode = 0; // 用于用汉字宽度计算时 int piontW = font.charWidth('.') * 3; // 计算一个点的宽度 boolean lastCode = true; // 全部显示出来时为true int maxLength = maxWidth ; int textLength = font.stringWidth(text); if (textLength <= maxLength) {// 如果一行容得下时 v.addElement(text); return v; } else { // this.maxRow=textLength/maxLength+2; System.out.println("maxRow=" + maxRow); char[] chars = text.toCharArray(); // 转化成字符数组 int charWidth = 0; // 字符宽度 int rowLength = 0; // 每行的长度 int rowIndex = 0; // 每行开始的index boolean changeRow = false; for (int i = 0; i < chars.length; i++) { if (chars == '/n') { if (rowLength > 0) { v.addElement(text.substring(rowIndex, i)); rowLength = 0; } else v.addElement(""); i++; rowIndex = i; continue; } charWidth = font.charWidth(chars); if (!changeRow) fcwCode += charWidth; else changeRow = false; rowLength += charWidth; if (this.fontCountW > 0 && fcwCode > this.fontCountW) { int j = font.charWidth('.'); int w = rowLength - charWidth; if (w > maxLength || (w + j) > maxLength || (w + 2 * j) > maxLength || (w + 3 * j) > maxLength) { v.addElement(text.substring(rowIndex, i)); // v.addElement(new String(chars,rowIndex,i-rowIndex)); v.addElement("..."); } else { v.addElement(text.substring(rowIndex, i) + "..."); 4000 // v.addElement(new // String(chars,rowIndex,i-rowIndex)+"..."); } break; } if (rowLength > maxLength) { changeRow = true; if (chars >= 'A' && chars <= 'z') { // 如果下行第一个为字母时 if (i > 0 && chars[i - 1] >= 'A' && chars[i - 1] <= 'z') { int enIndex = 0; // 单词开始的index for (int j = i; j > 0 && chars[j - 1] >= 'A' && chars[j - 1] <= 'z'; j--) enIndex = j - 1; if (enIndex > rowIndex) { v.addElement(text.substring(rowIndex, enIndex)); // v.addElement(new // String(chars,rowIndex,enIndex-rowIndex)); // 把不是字母的放在一行中 rowIndex = enIndex; // 下一行的开始位置 i = enIndex--; rowLength = 0; } else { v.addElement(text.substring(rowIndex, i)); // v.addElement(new // String(chars,rowIndex,i-rowIndex)); // 每行的文本 rowIndex = i; // 下一行的开始位置 i--; // 起点从下一行第一个开始 rowLength = 0; } } } else { v.addElement(text.substring(rowIndex, i)); // v.addElement(new String(chars,rowIndex,i-rowIndex)); // 每行的文本 rowIndex = i; // 下一行的开始位置 i--; // 起点从下一行第一个开始 rowLength = 0; } } if (i < chars.length - 1) lastCode = false; if (this.maxrowCode && v.size() == this.maxRow) break; } if (this.fontCountW <= 0) { if (rowIndex <= chars.length - 1) { // 最后一行不够文本时 boolean h = false; if (maxRow > 0 && v.size() == this.maxRow) { String vLast = (String) v.lastElement(); if (!lastCode) { char[] chs = vLast.toCharArray(); int chsLength = 0; if (null != chs) chsLength = chs.length; if (chsLength > 0) { if (font.charWidth(chs[chsLength - 1]) > piontW) // vLast=(new // String(chs,0,chsLength-1))+"..."; // 如果最后一个字符的宽度大于三个点的宽度时 vLast = vLast.substring(0, chsLength - 1); else { if (chsLength > 1 && font .charWidth(chs[chsLength - 1]) + font .charWidth(chs[chsLength - 2]) > piontW) // vLast=(new // String(chs,0,chsLength-2))+"..."; // 如果最后两个字符的宽度都大于三个点的宽度时 vLast = vLast.substring(0, chsLength - 2) + "..."; else // vLast=(new // String(chs,0,chsLength-3))+"..."; // 其它的按三个字符来算 vLast = vLast.substring(0, chsLength - 3) + "..."; } v.removeElementAt(v.size() - 1); v.addElement(vLast); } } } else { // v.addElement(new // String(chars,rowIndex,chars.length-rowIndex)); String vLast = text.substring(rowIndex, chars.length); char[] chs = vLast.toCharArray(); int r = 0; for (int i = 0; i < chs.length; i++) { if (chs == '/n') { h = true; v.addElement(vLast.substring(r, i)); i++; r = i; } } if (h) { v.addElement(vLast.substring(r, chs.length)); } else v.addElement(vLast); } } } } System.out.println("list size=" + v.size()); System.out.println(v.elementAt(0)); System.out.println(v.elementAt(5)); return v; } /** * @ description:覆盖父类中的getTextAt方法 @ author Lancelothe @ parameter @ create_date 2010-9-30 @ update_date * 2010-9-30 * @see com.sun.lwuit.TextArea#getTextAt(int) */ public String getTextAt(int line) { Vector rowsV = this.getRowStrings(); return (String) rowsV.elementAt(line); } public int getMaxRow() { return maxRow; } public void setMaxRow(int maxRow) { this.maxRow = maxRow; this.initRowString(); } public int getFontCountW() { return fontCountW; } public void setFontCountW(int fontCount) { this.fontCountW = fontCount * this.fontFx; this.initRowString(); this.setPreferredSize(new Dimension(this.autoWidth, calcPreferredSize() .getHeight())); } } |
相关文章推荐
- 解决表格自动换行问题:让表格宽度根据内容长度自动伸展
- PHP 根据文字内容添加图片上实现自动换行的小程序
- android 流布局(根据内容宽度自动换行的布局)
- 继承LinearLayout实现根据屏幕宽度及内部子View个数自动排布GridView
- ios 文字 控件 自适应 高度 宽度 CustomCell 自适应高度+uilabel自动换行+ UITextView 根据内容自动调整高度
- 重写java中JButton,实现图片下显示文字(根据图片宽度文字自动换行)
- 在火狐和chrome不同的自动填充密码机制下实现根据密码框是否有内容来添加不同类的功能
- 实现屏幕变化 div自动变化而且不换行的功能
- html内容超出了div的宽度如何换行让内容自动换行
- CSS布局 — 根据内容是否自动伸缩展示区域的那些元素
- table中表格内的内容自动换行不改变表格宽度
- 利用ul li 标签实现table表格的效果 (li内容超出ul设定的宽度时不换行)
- 根据首字母自动查找功能的实现
- UIWebView 实现图片适应屏幕的宽度 文字内容的大小不变
- #CSS#在设定table为百分比时,如何实现火狐,Chrome下TD内容文字超出时自动换行?
- 单行显示的UILabel根据文字内容自动调整宽度,文字下方加下划线
- eclipse实现根据DTD文件自动提示功能
- 用ViewGroup实现多View自动换行的功能
- java实现记事本基本功能:新建、打开、保存、另存为、退出、替换、全部替换、复制、剪切、粘贴、全选、获取当前时间日期、修改字体、颜色、自动换行等
- J2ME简单灵活实现手机中自动换行显示文本