2014-12-07 58 views
0

因此,我搜索扔所有的getHeight/widht問題帖子,並嘗試了不同的方法來解決問題(如globalLayoutListener,或Runnable),等待,直到FrameLayout已創建......但是我的的getHeight返回0 所以這是我的代碼現在:getHeight和getMeasuredHeight返回0,即使在Runnable

private void zurechtruecken() { 
    int n=1; 

    spB =(FrameLayout) findViewById(R.id.spielbereich); 

    spB.post(new Runnable(){ 
     @Override 
     public void run(){ 

      spielBh=spB.getMeasuredHeight(); 
      spielBb=spB.getMeasuredWidth(); 
     } 
    }); 

這是我的代碼與GlobalLayoutListener:

private void zurechtruecken() { 
    int i=24; 
    int n=1; 

    mView = findViewById(R.id.spielbereich); 
    mView.getViewTreeObserver().addOnGlobalLayoutListener( 
     new OnGlobalLayoutListener(){ 

      @Override 
      public void onGlobalLayout() { 
       mView.getViewTreeObserver().removeGlobalOnLayoutListener(this); 
       spielBh=mView.getHeight(); 
       spielBb=mView.getWidth(); 
       Log.i("!",Integer.toString(spielBh)); 
       Log.i("!",Integer.toString(spielBb)); 


      } 
     }); 

的「removeGlobalLayoutListener(本) 「被橫渡了(它被棄用)

這可能是重要的,那IM測量的幀結構


感謝您的幫助! Botti560

+0

是設置爲我而去之後沒有的FrameLayout的知名度? – 2014-12-07 12:36:06

+0

應該是? - 目前設置爲可見 – Botti560 2014-12-07 12:40:35

+0

您是否嘗試過使用onFinishInflate() – Varundroid 2014-12-07 12:41:04

回答

1

我也有過這樣的麻煩。如果一切都失敗了,請在您自己的類中擴展FrameLayout,並在onMeasure方法中獲取高度/寬度值。例如:

@Override 
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { 

    super.onMeasure(widthMeasureSpec, heightMeasureSpec); 

    int height = MeasureSpec.getSize(heightMeasureSpec); 

} 

容易把它在你的XML佈局太喜歡的東西:

<com.yourdomain.yourappname.FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" 
android:layout_height="match_parent" > ... 
+0

不錯!我只想試試 – Botti560 2014-12-07 12:58:24

+0

所以你的意思是說,我應該創建另一個類,來測量frameLayout?(對不起,我很新編程) – Botti560 2014-12-07 13:09:43

+0

或者我可以在onCreate之後調用zurechtruecken方法嗎?(但是也可以運行?) – Botti560 2014-12-07 13:13:00

相關問題