Android 如何检查textView是否被省略
我想知道如何检查我的文本视图是否被省略 这是我的代码片段:Android 如何检查textView是否被省略,android,nullpointerexception,textview,Android,Nullpointerexception,Textview,我想知道如何检查我的文本视图是否被省略 这是我的代码片段: if(txtDescription.equals("")) { txtDescription.setVisibility(View.GONE); } else if(txtDescription.getLayout().getEllipsisCount(1) > 0){ // this line is giving java.lang.NullPointerException txtDescription.se
if(txtDescription.equals("")) {
txtDescription.setVisibility(View.GONE);
} else if(txtDescription.getLayout().getEllipsisCount(1) > 0){ // this line is giving java.lang.NullPointerException
txtDescription.setVisibility(View.VISIBLE);
} else {
txtDescription.setVisibility(View.VISIBLE);
}
但是我总是得到一个java.lang.NullPointerException,我认为它来自getLayout()方法
这是TextView的XML:
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearance"
android:id="@+id/txtDescription"
android:ellipsize="end"
android:singleLine="true"
android:visibility="gone"
android:layout_marginTop="5dp" />
txtDescription.getLayout()
将返回null,因为您的textview布局尚未完成。
因此,您应该检查textView是否在onGlobalLayout
方法中被省略
ViewTreeObserver vto = textview.getViewTreeObserver();
vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
Layout l = txtDescription.getLayout();
if ( l != null){
// check your textview is being ellipsized or not here
...
}
}
});
txtDescription.getLayout()
将返回null,因为您的textview布局尚未完成。
因此,您应该检查textView是否在onGlobalLayout
方法中被省略
ViewTreeObserver vto = textview.getViewTreeObserver();
vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
Layout l = txtDescription.getLayout();
if ( l != null){
// check your textview is being ellipsized or not here
...
}
}
});
昨天我在尝试getLayout()时遇到了与NullPointerException相同的问题,并且在TextView的文档中发现Alhamdulillah发现这里有一个名为OnlLayout()的受保护方法,在TextView布局和渲染完成时调用该方法。在这之后,我扩展了AppCompatTextView(如果不使用appcompat库,也可以使用TextView)并重写它的onLayout()方法,然后将上下文强制转换到Activity类,并从该类调用一些公共方法
class TextViewForLayout extends AppCompatTextView {
Context mContext;
void SetContext(Context context)
{
mContext = context;
}
public TextViewForLayout(Context context) {
super(context);
SetContext(context);
}
public TextViewForLayout(Context context, AttributeSet attrs) {
super(context, attrs);
SetContext(context);
}
public TextViewForLayout(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
SetContext(context);
}
@Override
protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
super.onLayout(changed, left, top, right, bottom);
if (!changed) {
((MyActivity) mContext).someMethod();
}
}
}
在Activity类中,您需要有Ui线程处理程序和runnable作为全局变量,就像在我的例子中一样(或者通过传递参数使用runnable的另一种方式)
昨天我在尝试getLayout()时遇到了与NullPointerException相同的问题,并且在TextView的文档中发现Alhamdulillah发现这里有一个名为OnlLayout()的受保护方法,在TextView布局和渲染完成时调用该方法。在这之后,我扩展了AppCompatTextView(如果不使用appcompat库,也可以使用TextView)并重写它的onLayout()方法,然后将上下文强制转换到Activity类,并从该类调用一些公共方法
class TextViewForLayout extends AppCompatTextView {
Context mContext;
void SetContext(Context context)
{
mContext = context;
}
public TextViewForLayout(Context context) {
super(context);
SetContext(context);
}
public TextViewForLayout(Context context, AttributeSet attrs) {
super(context, attrs);
SetContext(context);
}
public TextViewForLayout(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
SetContext(context);
}
@Override
protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
super.onLayout(changed, left, top, right, bottom);
if (!changed) {
((MyActivity) mContext).someMethod();
}
}
}
在Activity类中,您需要有Ui线程处理程序和runnable作为全局变量,就像在我的例子中一样(或者通过传递参数使用runnable的另一种方式)