Cambiar posición de fondo / gravedad en ImageButton
Tengo la siguiente vista:
- Error Ningún recurso encontrado que coincida con el nombre dado (en 'src' con el valor '@ drawable / button1')
- Cómo cambiar la imagen de un botón con cada clic?
- ¿Cómo mantengo la relación de aspecto en los botones de imagen en android?
- ¿Cómo cambiar la imagen del botón de encendido a apagado y viceversa al hacer clic?
- Utilizar la misma ID para varios botones
Y quiero que se vea así:
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); ...
- ¿Cómo hacer que los límites de ImageButtons rodeen la imagen?
- Cómo utilizar múltiples TouchDelegate
- Cómo crear un botón de imagen en Android?
- ¿Cómo establecer un ImageButton para cambiar sólo cuando mi dedo está tocando
- Cómo tener un ImageButton transparente: Android
- Cómo colocar el botón en el interior del texto de edición
- ¿Puedo ocultar un botón de imagen en un diseño, (dimensiones y fondo) hasta que aparezca una llamada para establecerla?
- Diferencia entre botón y botón de imagen
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);