ImageButton con diferentes estados tamaño de las imágenes

¿Cómo puedo evitar que un ImageButton tenga un tamaño fijo? Los dibujos utilizados en el selector tienen diferentes tamaños, y quiero que el ImageButton para cambiar el tamaño de sí mismo para que coincida con el tamaño de la imagen.

He intentado adjustViewBounds sin éxito.

 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:padding="8dp" > <ImageButton android:id="@+id/picture_imagebutton" android:layout_width="wrap_content" android:layout_height="wrap_content" android:adjustViewBounds="true" android:src="@drawable/tab_background_selector" /> </RelativeLayout> 

En lugar de utilizar ImageButton , utilice ImageView porque ImageButton no puede ImageButton tamaño en base a ImageSize . ImageView es la mejor manera alternativa que puedo sugerir para su problema. Consigue esto por la siguiente manera:

Layout.xml:

  <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent"> <ImageView android:id="@+id/image" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/image_selection"/> </RelativeLayout> 

Image_selection.xml:

 <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:state_focused="true" android:state_pressed="true" android:drawable="@drawable/default_image"/> //default_image.png is displayed when the Activity loads. <item android:state_focused="false" android:state_pressed="true" android:drawable="@drawable/default_image_hover" /> //default_image_hover.png is an image displaed during Onclick of ImageView <item android:state_focused="true" android:drawable="@drawable/default_image_hover" /> <item android:state_focused="false" android:drawable="@drawable/default_image"/> </selector> 

SampleActivity.java:

  ImageView myImage= (ImageView)findViewById(R.id.image); myImage.setOnClickListener(this); 

Como ya sabes, no hay una manera fácil de establecer drawables en un selector, y no creo que haya una solución basada en XML para tu problema. Prueba esto.

 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent"> <ImageButton android:id="@+id/button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/default_image" android:contentDescription="@null"/> </RelativeLayout> 

A continuación, debe agregar este código a onCreate o onResume

  final ImageButton button = (ImageButton) findViewById(R.id.button); button.setOnTouchListener(new OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { if(event.getAction() == MotionEvent.ACTION_DOWN) button.setImageDrawable(getResources().getDrawable(getResources().getIdentifier("pressed_image", "drawable", getPackageName()))); else if(event.getAction() == MotionEvent.ACTION_UP) button.setImageDrawable(getResources().getDrawable(getResources().getIdentifier("default_image", "drawable", getPackageName()))); return false; } }); 

No utilice un ImageButton, simplemente utilice Image y suminístrelo con un método onClick

Tratar

 android:background="@drawable/tab_background_selector" 

Insted de

 android:src="@drawable/tab_background_selector" 

Espero que ayude.

Como otros han mencionado, usted será el mejor de usar un ImageView pues ajustará su altura. Establezca la propiedad setClickable para usarla como un botón. También intercambie su RelativeLayout por un LinearLayout , esto envolverá su altura a su ImageButton :

 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:padding="8dp" > <ImageView android:id="@+id/picture_imagebutton" android:layout_width="wrap_content" android:layout_height="wrap_content" android:adjustViewBounds="true" android:setClickable="true" android:src="@drawable/tab_background_selector" /> </LinearLayout> 

Alternativamente, puede hacer clic en el LinearLayout y agregar un recurso de fondo (transparente cuando no se hace clic, coloreado cuando se hace clic) para actuar como selector.

Puede utilizar estilos y establecer diferentes estilos basados ​​en el estado de ImageBUtton. Debido a que los estilos de apoyo de fondo, tamaño y muchas otras propiedades que le ayuda a obtener un control total de estilo dependiendo del problema a mano.

  • Animación de agitación de botón de Android
  • Diferencia entre botón y botón de imagen
  • ¿Diferencia entre el botón con la imagen, ImageButton, y clickable ImageView?
  • Android Studio: Utilizar ImageButton para iniciar una nueva actividad bloquea la aplicación
  • Cómo crear una cuadrícula de 10x10 en Android?
  • Capturar LinearLayout eventos onClick con ImageButton dentro de él
  • El relleno no funciona en ImageButton
  • Imagen de png que aparece con fondo gris en android
  • Android: ¿Cómo evitar las prensas fuera del círculo de un botón redondo utilizando el contenido?
  • Android imagebutton cómo cambiar el tamaño del icono
  • Error Ningún recurso encontrado que coincida con el nombre dado (en 'src' con el valor '@ drawable / button1')
  • FlipAndroid es un fan de Google para Android, Todo sobre Android Phones, Android Wear, Android Dev y Aplicaciones para Android Aplicaciones.