Cambiar posición de fondo / gravedad en ImageButton

Tengo la siguiente vista:

Introduzca aquí la descripción de la imagen

Y quiero que se vea así:

Introduzca aquí la descripción de la imagen

En el primero, el fondo del ImageButton verde está en la esquina superior izquierda. Y en el segundo, el fondo del ImageButton verde está en el centro. (La imagen de fondo está en el tamaño del cuadrado verde)

Aquí está mi código XML:

<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:layout_height="wrap_content"> <ImageButton android:id="@+id/dialer" android:layout_alignParentTop="true" android:layout_alignParentLeft="true" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@drawable/btncall" android:scaleType="centerInside"/> <Button android:id="@+id/sendSMS" android:layout_alignParentTop="true" android:layout_alignParentRight="true" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Send SMS"/> <Button android:id="@+id/four" android:layout_alignParentBottom="true" android:layout_alignParentLeft="true" android:layout_width="wrap_content" android:layout_height="wrap_content"/> <Button android:id="@+id/shutDownDevice" android:layout_alignParentBottom="true" android:layout_alignParentRight="true" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Turn Off"/> </RelativeLayout> 

Dónde

 android:background="@drawable/btncall" 

Se refiere a btncall.xml que se parece a esto:

 <?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:drawable="@drawable/phone_down" android:state_pressed="true" /> <item android:drawable="@drawable/phone_down" android:state_focused="true" /> <item android:drawable="@drawable/phone_up" /> </selector> 

Soy un principiante con Android, así que si ves mejor manera de crear este wiew, plz decirme.

10x mucho, Ryan.


En Addtion, esto es parte del código en el archivo principal de Java:

 ... //Get width and Height of screen Display display = getWindowManager().getDefaultDisplay(); int halfStageWidth = display.getWidth()/2; int halfStageHeight = display.getHeight()/2; //Get an instance of the Dialer button ImageButton btnDialer = (ImageButton) findViewById(R.id.dialer); Button btnSendSMS = (Button) findViewById(R.id.sendSMS); btnSendSMS.setWidth(halfStageWidth); btnSendSMS.setHeight(halfStageHeight); Button btnShutDownDevice = (Button) findViewById(R.id.shutDownDevice); btnShutDownDevice.setWidth(halfStageWidth); btnShutDownDevice.setHeight(halfStageHeight); Button B4 = (Button) findViewById(R.id.four); B4.setWidth(halfStageWidth); B4.setHeight(halfStageHeight); ... 

Lo estás haciendo de una manera que es demasiado complicado, esto puede hacerse sólo por XML sin necesidad de código java.

Su XML debe ser:

 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_weight="1" android:orientation="horizontal" > <ImageButton android:id="@+id/dialer" android:layout_width="fill_parent" android:layout_height="fill_parent" android:src="@drawable/btncall" android:background="@android:color/transparent" android:scaleType="centerInside" android:layout_weight="1"/> <Button android:id="@+id/sendSMS" android:layout_width="fill_parent" android:layout_height="fill_parent" android:text="Send SMS" android:layout_weight="1" /> </LinearLayout> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_weight="1" android:orientation="horizontal" > <Button android:id="@+id/four" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_weight="1"/> <Button android:id="@+id/shutDownDevice" android:layout_width="fill_parent" android:layout_height="fill_parent" android:text="Turn Off" android:layout_weight="1"/> </LinearLayout> 

No ha definido el tamaño del botón de imagen para que se ajuste alrededor de la imagen. Usted debe fijar el tamaño y debe centrar dentro del botón.

Actualizar:

Puede establecer un tamaño de diseño como este:

 RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(width,height); // Add relativeLayout rules here btnDialer.setLayoutParams(params); 
  • Android ImageButton frontera?
  • icono de botón de cambio de android cuando se hace clic
  • Android ImageButton con un estado seleccionado?
  • ¿Cuál es el valor de R.id.ImageButton?
  • ¿Qué debo usar ImageButton o Button?
  • Fondo transparente en ImageButton con efecto de ondulación?
  • Expectativa de puntero nulo al configurar LayoutParams
  • ¿Cómo extender ImageButton correctamente?
  • ¿Cómo puedo cambiar el tamaño de imageButton?
  • Android ImageButton - no se puede obtener la imagen en el centro
  • Android highlight ImageButton onclick
  • FlipAndroid es un fan de Google para Android, Todo sobre Android Phones, Android Wear, Android Dev y Aplicaciones para Android Aplicaciones.