una studio redondo poner imagen ejemplo diseño como cambiar botones boton ajustar java android xml togglebutton

java - studio - imagebutton android ejemplo



Android: especifique dos imágenes diferentes para togglebutton utilizando XML (1)

Tu código está bien. Sin embargo, el botón de alternar mostrará el primer elemento del selector que coincida, por lo que el valor predeterminado debería ser el último. Organice los artículos de la siguiente manera para garantizar que se utilicen todos:

<selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:state_checked="true" android:state_pressed="true" /> //currently pressed turning the toggle on <item android:state_pressed="true" /> //currently pressed turning the toggle off <item android:state_checked="true" /> //not pressed default checked state <item /> //default non-pressed non-checked </selector>

Estoy intentando anular la apariencia predeterminada de ToggleButton . Aquí está el XML que define el ToggleButton :

<ToggleButton android:id="@+id/FollowAndCenterButton" android:layout_width="30px" android:layout_height="30px" android:textOn="" android:textOff="" android:layout_alignParentLeft="true" android:layout_marginLeft="5px" android:layout_marginTop="5px" android:background="@drawable/locate_me"/>

Ahora, tenemos dos iconos de 30 x 30 que queremos usar para los estados en los que se hace clic o no se hace clic. En este momento tenemos un código que cambia programáticamente el icono de fondo dependiendo del estado:

centeredOnLocation.setOnClickListener(new OnClickListener() { public void onClick(View v) { if (centeredOnLocation.isChecked()) { centeredOnLocation.setBackgroundDrawable(getResources().getDrawable(R.drawable.locate_me_on)); } else { centeredOnLocation.setBackgroundDrawable(getResources().getDrawable(R.drawable.locate_me)); } } });

Obviamente estoy buscando una mejor manera de hacer esto. Intenté hacer un selector para la imagen de fondo, que cambiará automáticamente entre los estados:

<selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:drawable="@drawable/locate_me" /> <!-- default --> <item android:state_checked="true" android:drawable="@drawable/locate_me_on" /> <!-- pressed --> <item android:state_checked="false" android:drawable="@drawable/locate_me" /> <!-- unchecked -->

Pero esto no funciona; leyendo la API de ToggleButton ( http://developer.android.com/reference/android/widget/ToggleButton.html ), parece que los únicos atributos xml heredados son

XML Attributes Attribute Name Related Method Description android:disabledAlpha The alpha to apply to the indicator when disabled. android:textOff The text for the button when it is not checked. android:textOn The text for the button when it is checked.

No parece haber el atributo android: state_checked, a pesar de que la clase tenga el método isChecked() y setChecked() .

Entonces, ¿hay alguna manera de hacer lo que quiero en XML, o estoy atascado con mi complicada solución?