2011-11-20 9 views

Respuesta

37
Display display = getWindowManager().getDefaultDisplay(); 
View view = findViewById(R.id.YOUR_VIEW_ID); 
view.measure(display.getWidth(), display.getHeight()); 

view.getMeasuredWidth(); // view width 
view.getMeasuredHeight(); //view height 
+0

Por favor, marca como resuelto ^^ –

+4

También debes tener en cuenta que si la vista no está inflada, su ancho y alto será 0. – Joru

+0

Tienes razón Joru. Utilicé ViewTreeObserver y resolví mi problema. Gracias a todos los que intentaron responder. –

23

@dmytrodanylyk - creo que va a devolver la anchura & altura que 0 por lo que es necesario utilizar lo siguiente

LayoutInflater inflater = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
View contentview = inflater.inflate(R.layout.YOURLAYOUTNAME, null, false); 
contentview.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED); 
int width = contentview.getMeasuredWidth(); 
int height = contentview.getMeasuredHeight(); 

Se le dará altura correcta & ancho.

+0

Eso es lo que necesito para determinar si debo comenzar una nueva fila de casillas de verificación, primero haciendo que sea una sola línea y luego evitar el desbordamiento calculando el ancho ... perfecto gracias. viniendo de -> http://stackoverflow.com/a/24054428/1815624 – CrandellWS

9

Debe usar OnGlobalLayoutListener, que se solicita para cambios en la vista, pero antes de que se dibuje.

costumView.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener() { 

    @Override 
    public void onGlobalLayout() { 

     costumView.getWidth(); 

    } 
}); 
2

me gusta utilizar esta técnica como por my answer on a related question:

Publica ejecutable desde onCreate() que se ejecutará cuando el punto de vista ha sido creado:

contentView = findViewById(android.R.id.content); 
    contentView.post(new Runnable() 
    { 
     public void run() 
     { 
      contentHeight = contentView.getHeight(); 
     } 
    }); 

Este código se ejecutará en el El subproceso principal de interfaz de usuario, después de que onCreate() haya finalizado.

0

Esta es la mejor manera ...... TRABAJO !!!

public void onWindowFocusChanged(boolean hasFocus) {   
    super.onWindowFocusChanged(hasFocus); 

    if (fondo_iconos_height==0) { // to ensure that this does not happen more than once 
     fondo_iconos.getLocationOnScreen(loc); 
     fondo_iconos_height = fondo_iconos.getHeight(); 
     redraw_function(); 
    } 

} 
5
Display display = getWindowManager().getDefaultDisplay(); 
Point size = new Point(); 
display.getSize(size); 
view.measure(size.x, size.y); 
int width = view.getMeasuredWidth(); 
int height = view.getMeasuredHeight(); 
0
yourView.measure(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT); 
    int yourView= m_hidableRowContainer.getMeasuredHeight(); 

Eso es todo.

+0

'medida' toma un par de constantes enteras' View.MeasureSpec', no constantes de param de diseño. – Tom

Cuestiones relacionadas