TextView显示不全问题分析
2017-11-02 11:57
295 查看
TextView的singleLine和lines方法分析
记录问题欢迎指教。
我是用权重分割了TextView然后在TextView上做了单行的限制,可是在不同的数据上显示出不一样的效果:下面是我的TextView,这里除了使用权重外并没有其他的特别之处。
<TextView android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="2" android:ellipsize="end" android:gravity="center" android:singleLine="true" android:paddingLeft="5dp" android:paddingRight="5dp" android:textSize="20sp" />
五羊(FIVERAMS)婴儿洗发沐浴露二合一 哈哈哈哈哈哈 怀疑是这个名字中使用了中文括号导致。
然后我尝试修改文字长度都不能解决,但是去掉中文的括号后是可以解决的,非常奇怪。
上面并不能解决实际问题,所以要在继续找问题,把TextView的属性都去掉,最后发现问题在控制文字是不是单行的上面,我以前用的是android:maxLines=”1”,后来改成android:singleLine=”true”,居然就好了。
注意:在使用权重的时候尽量在里面多一层view,并且设置单行尽量使用setSingleLine(虽然提示我们已经过时的方法,),这样可以避免一些诡异的东西。
下面简单看一下源码
主要分析三个方法:setMaxLines() ; setLines();setSingleLine();(无参数默认是true)第一个和第二个方法类似,主要是设置模式和重新布局和刷新:
public void setLines(int lines) { mMaximum = mMinimum = lines; mMaxMode = mMinMode = LINES; requestLayout(); invalidate(); } @android.view.RemotableViewMethod public void setMaxLines(int maxlines) { mMaximum = maxlines; mMaxMode = LINES; requestLayout(); invalidate(); }
第三个方法就复杂很多,设置了除上面参数外的其他东西:
public void setSingleLine() { setSingleLine(true); } @android.view.RemotableViewMethod public void setSingleLine(boolean singleLine) { // Could be used, but may break backward compatibility. // if (mSingleLine == singleLine) return; //这里调用了两个方法,第一个可以看出,是设置输入类型(edittext使用),第二个是应用单行模式 setInputTypeSingleLine(singleLine); applySingleLine(singleLine, true, true); } /** * Adds or remove the EditorInfo.TYPE_TEXT_FLAG_MULTI_LINE on the mInputType. * @param singleLine */ private void setInputTypeSingleLine(boolean singleLine) { if (mEditor != null && (mEditor.mInputType & EditorInfo.TYPE_MASK_CLASS) == EditorInfo.TYPE_CLASS_TEXT) { //这设置mEditor的输入方式 if (singleLine) { mEditor.mInputType &= ~EditorInfo.TYPE_TEXT_FLAG_MULTI_LINE; } else { mEditor.mInputType |= EditorInfo.TYPE_TEXT_FLAG_MULTI_LINE; } } }
下面这个applySingleLine方法是重点了:
private void applySingleLine(boolean singleLine, boolean applyTransformation, boolean changeMaxLines) { mSingleLine = singleLine; //这里调用了设置单行的方法,设置可以横向滚动 if (singleLine) { setLines(1); setHorizontallyScrolling(true); if (applyTransformation) { setTransformationMethod(SingleLineTransformationMethod.getInstance()); } } else { if (changeMaxLines) { setMaxLines(Integer.MAX_VALUE); } setHorizontallyScrolling(false); if (applyTransformation) { setTransformationMethod(null); } } }
在下面是另外的主要方法setTransformationMethod,调用了setText()把文字设置进去了:
public final void setTransformationMethod(TransformationMethod method) { if (method == mTransformation) { // Avoid the setText() below if the transformation is // the same. return; } //对字符串进行了处理 if (mTransformation != null) { if (mText instanceof Spannable) { ((Spannable) mText).removeSpan(mTransformation); } } mTransformation = method; if (method instanceof TransformationMethod2) { TransformationMethod2 method2 = (TransformationMethod2) method; mAllowTransformationLengthChange = !isTextSelectable() && !(mText instanceof Editable); method2.setLengthChangesAllowed(mAllowTransformationLengthChange); } else { mAllowTransformationLengthChange = false; } setText(mText); if (hasPasswordTransformationMethod()) { notifyViewAccessibilityStateChangedIfNeeded( AccessibilityEvent.CONTENT_CHANGE_TYPE_UNDEFINED); } // PasswordTransformationMethod always have LTR text direction heuristics returned by // getTextDirectionHeuristic, needs reset mTextDir = getTextDirectionHeuristic(); }
上面可以看出setSingleLine()做了很多的工作,有一些并不是和TextView相关。对字符串进行了一些处理。
这也是三个方法中很重要的区别,猜想是这里引起的。
相关文章推荐
- 使用WebView.load(data,"text/html", "utf-8")加载显示乱码问题分析
- Android textview 显示不全的问题
- ListView嵌套ListView,TextView有多行文本显示不全不问题
- iphone--设置UITextView内边距,解决textView内容显示不全的问题
- android TextView 显示不全的问题解决,此问题是设置了maxLines和ellipsize=end情况下发生的!
- textview显示文字不全问题
- 重写TextView解决HorizontalScrollView中TextView显示不全的问题
- TextView设置文本部分可点击时出现文本不显示的问题分析与解决
- 【完美解决系列】Android在自定义Dialog中TextView内容显示不全的问题
- 在TextView中设置DrawableLeft不显示的问题
- 关于TextView限制显示字符,多余用省略号显示问题
- Android 使用ViewPager Indicator时,标题显示不全的问题
- Vitamio使用时,VideoView自动向上弹导致VideoView显示不全问题的完美解决方案
- TextView显示颜色高亮的问题
- 信息异步处理,关于handle和thread交互信息,只能更改一个textview的问题原因分析
- 问题:textview的内容不顶格显示
- 【菜鸟学安卓】- TextView 显示Html 二 图片大小的显示问题
- 问题:textview的内容不顶格显示
- 当ListView显示不全时,除了总体测量外还要测量TextView的高度
- TextView的setCompoundDrawables不显示问题解决