2016-06-22 6 views
0

내 텍스트 뷰가 ellipsized되고 있는지 확인하는 방법을 알고 싶어요 ..텍스트 뷰 내 코드의 안드로이드

ellipsized되고 있는지 여부를 확인하는 방법 :

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); 
} 

하지만 난 항상 자바를 얻을 수 있습니다. lang.NullPointerException 나는 ..이 getLayout() 메소드에서 오는 생각 텍스트 뷰의 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" /> 

답변

0

txtDescription.getLayout()은 textview 레이아웃이 완료되지 않았기 때문에 null을 반환합니다. 따라서 당신은 텍스트 뷰가

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 
       ... 
       }   
     } 
}); 
0

어제 같은 onGlobalLayout 방법 내부 ellipsized되는 경우 나 보호 여기 발견 텍스트 뷰의 문서에 getLayout()와 Alhamdulillah 시도하면서 NullPointerException이과 같은 문제가 확인해야 메서드 onLayout()이고 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(); 
    } 
} 
} 

당신은 UI 스레드 핸들러 내 경우 글로벌 변수로 실행 가능한이 (또는 인수를 전달하여보다 Runnable를 사용하는 다른 방법을 사용)

public class MyActivity extends AppCompatActivity { 
    Handler mainHandler; 
    Runnable addViewTo; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.mylayout); 

     mainHandler = new Handler(); 

     addViewTo = new Runnable() { 
     @Override 
     public void run() { 
       // add single view or iterate throug List, Array or some Collections 
       someParent.addView(childView); 
      } 
     }; 
    } 

    public void someMethod() 
    { 
     // do something with textView.getLayout()..... 
     // and post the addViewTo runnable to mainHadler 
     // or the manipulations with UI will not shown 
    } 
} 
필요
관련 문제