어제 같은 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
}
}
필요