¿Establecer el ancho y la altura de ImageView mediante programación?


¿Cómo puedo establecer un ancho y alto de ImageView mediante programación?

Author: blahdiblah, 2010-06-30

9 answers

Puede ser demasiado tarde, pero por el bien de otros que tienen el mismo problema, para establecer la altura de la ImageView:

image_view.getLayoutParams().height = 20;

Espero que esto ayude.

Importante. Si está configurando la altura después de que el diseño ya haya sido 'diseñado', asegúrese de llamar también a:

image_view.requestLayout()
 930
Author: Hakem Zaied,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2015-03-30 18:49:30

Si su vista de imagen es dinámica, la respuesta que contiene getLayout fallará con null-exception.

En ese caso la forma correcta es:

LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(100, 100);
iv.setLayoutParams(layoutParams);
 202
Author: InvulgoSoft,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2012-03-26 17:21:16

Esta forma sencilla de hacer tu tarea:

setContentView(R.id.main);    
ImageView iv = (ImageView) findViewById(R.id.left);
int width = 60;
int height = 60;
LinearLayout.LayoutParams parms = new LinearLayout.LayoutParams(width,height);
iv.setLayoutParams(parms);

Y otra forma si desea dar tamaño de pantalla en altura y ancho, utilice el siguiente código:

setContentView(R.id.main);    
Display display = getWindowManager().getDefaultDisplay();
ImageView iv = (LinearLayout) findViewById(R.id.left);
int width = display.getWidth(); // ((display.getWidth()*20)/100)
int height = display.getHeight();// ((display.getHeight()*30)/100)
LinearLayout.LayoutParams parms = new LinearLayout.LayoutParams(width,height);
iv.setLayoutParams(parms);

La esperanza de uso completo para usted.

 43
Author: duggu,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2013-01-11 08:38:48

He jugado este con pixel y dp.

private int dimensionInPixel = 200;

Cómo establecer por pixel:

view.getLayoutParams().height = dimensionInPixel;
view.getLayoutParams().width = dimensionInPixel;
view.requestLayout();

Cómo establecer por dp:

int dimensionInDp = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dimensionInPixel, getResources().getDisplayMetrics());
view.getLayoutParams().height = dimensionInDp;
view.getLayoutParams().width = dimensionInDp;
view.requestLayout();

Espero que esto te ayude.

 26
Author: Hiren Patel,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2016-05-24 09:41:42

Si necesita establecer su ancho o alto en match_parent (tan grande como su padre) o wrap_content (lo suficientemente grande como para adaptarse a su propio contenido interno), entonces ViewGroup.LayoutParams tiene estas dos constantes:

imageView.setLayoutParams(
    new ViewGroup.LayoutParams(
        // or ViewGroup.LayoutParams.WRAP_CONTENT
        ViewGroup.LayoutParams.MATCH_PARENT,      
        // or ViewGroup.LayoutParams.WRAP_CONTENT,     
        ViewGroup.LayoutParams.MATCH_PARENT ) );

O puedes ponerlos como en la respuesta de Hakem Zaied :

imageView.getLayoutParams().width = ViewGroup.LayoutParams.MATCH_PARENT;
//...
 11
Author: Omar,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2017-05-23 12:34:53

Puede establecer un valor para todos los casos.

demoImage.getLayoutParams().height = 150;

demoImage.getLayoutParams().width = 150;

demoImage.setScaleType(ImageView.ScaleType.FIT_XY);
 9
Author: tienanhcntt2,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2016-07-27 14:21:45

La mejor y más fácil manera de establecer un botón dinámicamente es

 Button index=new Button(this);
 int height = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 45, getResources().getDisplayMetrics());             
 int width = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 42, getResources().getDisplayMetrics());

La altura y el ancho anteriores están en píxeles px. 45 la altura en dp y 42 siendo el ancho en dp.

 index.setLayoutParams(new <Parent>.LayoutParams(width, height));

Así que, por ejemplo, si has colocado tu botón dentro de una TableRow dentro de una TableLayout, deberías tenerlo como TableRow.LayoutParams

 index.setLayoutParams(new TableRow.LayoutParams(width, height));
 8
Author: Ali Kahoot,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2015-12-06 09:22:42

Simplemente crea un objeto LayoutParams y asígnalo a tu ImageView

LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(150, 150);
imageView.setLayoutParams(params);
 6
Author: Zubair Akber,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2016-01-05 06:04:50
image_view.getLayoutParams().height 

Devuelve el valor de altura en píxeles. Primero debe obtener las dimensiones de la pantalla para establecer los valores correctamente.

 Display display = context.getWindowManager().getDefaultDisplay();
 DisplayMetrics outMetrics = new DisplayMetrics();
 display.getMetrics(outMetrics);
 int screen_height = outMetrics.heightPixels;
 int screen_width = outMetrics.widthPixels;

Después de obtener las dimensiones de la pantalla, puede establecer el ancho y alto de imageview utilizando los márgenes adecuados.

 view.getLayoutParams().height = screen_height - 140;
 view.getLayoutParams().width = screen_width - 130 ;
 4
Author: Vikas,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2015-10-01 05:59:45