【问题标题】:Android ToggleButton安卓切换按钮
【发布时间】:2012-09-27 12:08:00
【问题描述】:

通过关注article,我能够创建一个由图像组成的切换按钮。我的切换没有任何文字,只有开/关图像。

当我的切换按钮被创建时,它被拉伸并失去了它的比例,我如何让它保持原来的大小?

这些是我使用的图像:

代码:

main.xml:

<ToggleButton
   android:id="@+id/changeNumerals"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:layout_alignParentLeft="true"
   android:layout_alignParentTop="true"
   android:checked="true"
   android:background="@drawable/toggle_bg"
   android:textOn=""
   android:textOff=""            
/>

drawable/toggle.xml:

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

drawable/toggle_bg.xml:

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
   <item android:id="@+android:id/background" android:drawable="@android:color/transparent" />
   <item android:id="@+android:id/toggle" android:drawable="@drawable/toggle" />
</layer-list>

【问题讨论】:

  • 对切换按钮的高度和宽度使用环绕内容
  • wrap_content 已经存在...
  • 你在使用九个补丁图像吗??
  • 图像是没有 9patch 黑线的常规 PNG,由于它们的圆形部分,不确定这些图像是否可以使用 9patch 正确拉伸。

标签: android view


【解决方案1】:

尝试&lt;ToggleButton&gt;的以下属性

android:background="@android:color/transparent"
android:button="@drawable/toggle_bg"

它应该工作。祝你好运:)

【讨论】:

  • 这会将图像以正确的大小放在按钮的顶部,但切换的背景是拉伸的切换图像。我尝试删除 android:background="@drawable/toggle_bg" 删除了图像并带回了常规切换按钮,上面有我的切换图像。
  • 我如何以编程方式添加此属性? android:button="@drawable/toggle_bg"
  • 你可以使用其中任何一个 setButtonDrawable(int resid) setButtonDrawable(Drawable d) developer.android.com/reference/android/widget/…
  • 谢谢!!!由于硬编码的大小,我的切换按钮在我的其他图像按钮旁边看起来很时髦,这为我解决了问题!
【解决方案2】:

尺寸是“硬编码”的,但它们会随着您的屏幕大小而缩放。

所以 xml 可能看起来像:

android:layout_width="64dp"
android:layout_height="24dp"

dps的官方文档是here

【讨论】:

    【解决方案3】:

    为了支持从 GINGERBREAD 到当前版本,我最终使用了:

    <ToggleButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/filterPhotos"
        android:checked="true"
        android:textOn=""
        android:textOff=""
        android:paddingLeft="5dp"
        android:drawableRight="@drawable/toggle_filter_photos"
        android:background="@null"/>
    

    以及可绘制的 toggle_filter_photos.xml:

    <selector xmlns:android="http://schemas.android.com/apk/res/android">
        <item android:drawable="@drawable/picture_filter_icons"
              android:state_checked="true" />
        <item android:drawable="@drawable/picture_inactive_filter_icons" />
    </selector>
    

    上述其他解决方案不适用于 GINGERBREAD 和更新版本,因为图标根本不会显示(在 GINGERBREAD 上使用 android:button)或纵横比丢失(在 JELLY_BEAN_MR1 上使用 android:background (4.2 .2))。

    【讨论】:

    • 评论最后为我工作“android:button on GINGERBREAD”..thanks
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-01-18
    • 2019-04-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多