2011-08-15 25 views
6

Tengo una vista que se debe mostrar en una posición específica sobre algunas imágenes, ya lo hice usando AbsoluteLayout, pero no quiero usarlo y estoy tratando de obtener el mismo resultado con un FrameLayout.Cambiar la posición absoluta con FrameLayout

Pero no puedo conseguir este trabajo, esto es mis dos intentos:

public void showVideo(RectF bounds) { 
    FrameLayout.LayoutParams params = (FrameLayout.LayoutParams) video.getLayoutParams(); 
    params.width = (int) bounds.width(); 
    params.height = (int) bounds.height(); 
    int x = (int) viewer.getPageXOffset(); 
    int y = (int) viewer.getPageYOffset(); 

    video.setPadding((int) (x + bounds.left), (int) (y + bounds.top), (int) (x + bounds.right), 
      (int) (y + bounds.bottom)); 

    video.setLayoutParams(params); 
    video.invalidate(); 

    video.setVisibility(View.VISIBLE); 
    video.setFocusable(true); 
    video.setFocusableInTouchMode(true); 
    video.requestFocus(); 
} 

Y:

public void showVideo(RectF bounds, final String videoUrl) { 
    FrameLayout.LayoutParams params = (FrameLayout.LayoutParams) video.getLayoutParams(); 
    params.width = (int) bounds.width(); 
    params.height = (int) bounds.height(); 
    int x = (int) (viewer.getPageXOffset() + bounds.left); 
    int y = (int) (viewer.getPageYOffset() + bounds.top); 

    params.leftMargin = x; 
    params.topMargin = y; 

    video.setLayoutParams(params); 
    video.invalidate(); 

    video.setVisibility(View.VISIBLE); 
    video.setFocusable(true); 
    video.setFocusableInTouchMode(true); 
    video.requestFocus(); 
} 

Pero en ambos casos, el VideoView se muestran en v (0,0) (esquina superior izquierda).

+0

¿Es necesario crear el diseño dinámicamente en el código? Nunca he tenido este tipo de problemas cuando trabajo con FrameLayout en XML – Egor

+0

En realidad, necesito mostrar solo una Vista que aparece sobre la actual (sin Absolute Lay) en una posición específica, y esta posición se calcula en tiempo de ejecución. –

+1

también ver 'view.setTranslationX()' o 'view.offsetLeftAndRight()' –

Respuesta

17

Debería funcionar cuando configura la gravedad en LayoutParams.

Trate

params.gravity = Gravity.LEFT | Gravity.TOP; 

para empezar.

Cuestiones relacionadas