En esta nueva ocasion les traigo un tema un tanto corto pero de mucha utilidad, que consiste en un codigo para ubicar nuestros elementos en nuestra aplicacion.
Basta con copiar la siguiente funcion:
public void AutoSizeAndSetXY(View v,double posX,double posY,double width,double height){
Display display = getWindowManager().getDefaultDisplay();
int widthP = display.getWidth();
int heightP = display.getHeight();
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
lp.setMargins(((int)Math.round(posX*widthP)),((int)Math.round(posY*heightP)), 1, 1);
v.setLayoutParams(lp);
v.getLayoutParams().height=(int)(Math.round(height*heightP));
v.getLayoutParams().width=(int)(Math.round(width*widthP));
}
y dentro del onCreate ubicamos nuestros elementos de la siguiente forma:
setContentView(R.layout.activity_control_aire);
final ImageButton btnarribaaire=(ImageButton) findViewById(R.id.imageButtonArriba);
final ImageButton btnabajoaire=(ImageButton) findViewById(R.id.imageButtonAbajo);
final ImageButton btnaspas=(ImageButton) findViewById(R.id.imageButtonAspas);
final ImageButton btnhumedad=(ImageButton) findViewById(R.id.imageButtonhumedad);
final ImageButton btnmaximo=(ImageButton) findViewById(R.id.imageButtonmaximo);
final ImageButton btnonaire=(ImageButton) findViewById(R.id.imageButtononaire);
final ImageButton btnventilador=(ImageButton) findViewById(R.id.imageButtonventilador);
//AutoSizeAndSetXY(elemento, posición en x, posición en y , ancho , alto);
AutoSizeAndSetXY(btnarribaaire, 0.3, 0.3333 , 0.3 , 0.2);
AutoSizeAndSetXY(btnabajoaire, 0.3, 0.5333 , 0.3 , 0.2 );
AutoSizeAndSetXY(btnaspas, 0, 0.3333 , 0.3 , 0.2);
AutoSizeAndSetXY(btnhumedad, 0, 0.5333 , 0.3 , 0.2 );
AutoSizeAndSetXY(btnmaximo, 0.6, 0.3333 , 0.3 , 0.2);
AutoSizeAndSetXY(btnonaire, 0, 0 , 0.3 , 0.2 );
AutoSizeAndSetXY(btnventilador, 0.6, 0.5333 , 0.3 , 0.2);
Los valores comprede rango entre 0 y 1.
No hay comentarios:
Publicar un comentario