【问题标题】:height difference between android togglebutton class and togglebutton xmlandroid togglebutton类和togglebutton xml之间的高度差
【发布时间】:2011-11-05 07:12:06
【问题描述】:

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

t按钮

    <?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 中(当然是片段)

    <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 的高度不同......就像顶部和底部有更多的填充一样。

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

谢谢, 凯莉

【问题讨论】:

    标签: android xml class height togglebutton


    【解决方案1】:

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

    你应该使用的是:

        getBackground().getBounds()
    

    这样做将允许您根据当前项目的约束(在本例中为 ToggleButton)调整您想要绘制的任何内容的大小和位置

    【讨论】:

      猜你喜欢
      • 2015-11-15
      • 1970-01-01
      • 2013-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-01-14
      • 2014-02-01
      相关资源
      最近更新 更多