android togglebutton类和togglebutton xml之间的高度差

时间:2011-08-28 00:08:20

标签: android xml class height togglebutton

所以我开始考虑扩展ToggleButton,这样我除了在选择器中使用状态之外,我还想在它上面绘制。正如我所说的那样,我意识到高度和layout_height似乎存在问题,我可能会遗漏一些东西......

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>

现在布局xml(当然是snippit)

    <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"/>

在构造函数中创建的codeToggle(没有什么花哨的)

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

你会发现codeToggle的高度与ToggleButton的高度不同......就像在顶部和底部有更多的填充。

为什么会这样,你会如何纠正它?

谢谢, 凯利

1 个答案:

答案 0 :(得分:0)

我发现解决方案相当简单,因为我没有找到正确的宽度和高度位置。

你应该使用的是:

    getBackground().getBounds()

执行此操作将允许您根据当前项目的约束(在本例中为ToggleButton)调整您想要绘制的任何内容和位置