您的位置:首页 > 其它

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相关。对字符串进行了一些处理。

这也是三个方法中很重要的区别,猜想是这里引起的。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  textview 源码分析