2011-10-15 18 views

Respuesta

13

Puede agregar un árbol observador al diseño. Esto debería devolver el ancho y la altura correctos. Se llama a onCreate antes de que se realice el diseño de las vistas secundarias. Por lo tanto, el ancho y la altura aún no se calculan. Para obtener el alto y el ancho Poner esto en el método onCreate

LinearLayout layout = (LinearLayout)findViewById(R.id.YOUD VIEW ID); 
ViewTreeObserver vto = layout.getViewTreeObserver(); 
vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener() { 
    @Override 
    public void onGlobalLayout() { 
     this.layout.getViewTreeObserver().removeGlobalOnLayoutListener(this); 
     int width = layout.getMeasuredWidth(); 
     int height = layout.getMeasuredHeight(); 

    } 
}); 
+1

FYI: 'removeGlobalOnLayoutListener' está en desuso en las API más recientes, y es' removeOnGlobalLayoutListener' ahora. Para más información: http://stackoverflow.com/a/16190337/487940 – harsimranb

Cuestiones relacionadas