diferencia de altura entre la clase togglebutton de Android y togglebutton xml

So I started looking at extending the ToggleButton so I could in addition to using states in the selector, I wanted to also draw on top of it. As I was going though this I realized that there seems to be an issue with the height and layout_height, which I might be missing something...

tbutton

    <?xml version="1.0" encoding="utf-8"?>
    <selector xmlns:android="http://schemas.android.com/apk/res/android">
        <item android:state_checked="true" android:drawable="@drawable/on"/>
        <item android:state_checked="false" android:drawable="@drawable/off"/>
    </selector>

now inside the layout xml (snippit of course)

    <ToggleButton android:id="@+id/mytoggle1"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:background="@drawable/tbutton"
              android:textOn=""
              android:textOff=""
              android:layout_weight="0"
              android:focusableInTouchMode="false"
              android:focusable="false"
              android:layout_marginRight="@dimen/PADDING"/>

    <edu.ninjacore.components.codeToggle android:id="@+id/mytoggle2"
                         android:layout_height="wrap_content"
                         android:layout_width="wrap_content"
                         android:focusable="false"
                         android:focusableInTouchMode="false"
                         android:layout_weight="0"/>

in the create of codeToggle I have in the constructor (nothing fancy)

    public codeToggle(Context context)
    {
        super(context);
        setBackgroundDrawable(getResources().getDrawable(R.drawable.tbutton));
    }

What you'll find is that the codeToggle will have a different height than the ToggleButton height... it's like there is a slew more padding on the top and bottom.

Why is that, and how would you correct it?

Gracias Kelly

preguntado el 28 de agosto de 11 a las 00:08

1 Respuestas

I found out the solution was rather simple, as I wasn't going to the right location for width and height.

What you should use is the:

    getBackground().getBounds()

Doing this it will allow you to resize whatever you want to draw and position based on the constraints of the current item (in this case the ToggleButton)

Respondido el 05 de Septiembre de 11 a las 22:09

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.