2012-01-11 56 views
-1

在我的應用程序中,我有ViewFlipper中的選項卡。 當活動開始時,我初始化所有選項卡及其內部的視圖。Android的ViewFlipper初始化視圖

但問題出在第3個選項卡上。在此選項卡的初始化過程中,我想知道TextView與父母左側的距離。爲此我稱之爲TextView.getLeft()。

但是,當這個標籤沒有顯示,我收到這個方法的'0'。因此,我無法正確初始化視圖。

現在變得奇怪: 當我開始活動,第一個選項卡,然後去第三,啓動的getLeft返回0,並在選項卡中單擊.... 但是當我開始活動到第三getLeft返回一個有效值....

那麼,我怎樣才能得到正確的值? 我猜這個ViewFlipper在選項卡沒有顯示時沒有正確的X,Y座標視圖。

Ps。這很難解釋。如果我需要澄清一些事情,請告訴我。

+0

其中是TextView.getLeft()。叫什麼名字?在onCreate? – blessenm 2012-01-11 09:17:42

+0

超時後:onCreate->啓動畫面 - >超時 - > setContentView(viewflipper) - > findViewById - > getLeft() – 2012-01-11 09:20:18

回答

0

我注意到getLeft之前被調用過!該選項卡實際上已被執行。 (電話是在setDisplayedChild()之後)

因此,我需要知道何時實際顯示視圖。 我加的InAnimation的AnimationListener:

ViewFlipper.getInAnimation().setAnimationListener(new AnimationListener() { 

    public void onAnimationStart(Animation animation) { 
     TextView.getLeft(); 
    } 

    public void onAnimationRepeat(Animation animation) {} 
    public void onAnimationEnd(Animation animation) {} 
}); 

我真的不喜歡這個sollution。但它的工作。

0

試試這個在上創建

ViewTreeObserver vto = textview.getViewTreeObserver(); 
vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener() { 
    @Override 
    public void onGlobalLayout() { 
     textview.getViewTreeObserver().removeGlobalOnLayoutListener(this); 
     Log.e("Width",Integer.toString(textview.getWidth())); 

    } 
}); 

的onCreate被稱爲繪製的UI之前,所以寬度應爲0的措施或圖紙還沒有發生。

+0

謝謝,我試過這個只在活動開始時調用。當標籤翻轉時不是。我仍然收到0. – 2012-01-11 12:33:26

+0

刪除此行textview.getViewTreeObserver()。removeGlobalOnLayoutListener(this);.或者可能需要嘗試一些TabChangedListener。 – blessenm 2012-01-11 12:42:02