简体   繁体   English

自定义切换按钮:如何防止文本与图标重叠

[英]Custom Toggle Button: How to prevent text overlapping with icon

I'm trying to make a custom toggle button, where the icon is considerably larger than the original built-in one. 我正在尝试制作一个自定义切换按钮,其图标比原始内置按钮大得多。 I'd like to achieve something like this: 我想实现以下目标:

    *-------------------*
    *  *------------*   *
    *  *            *   *
    *  *   Icon     *   *
    *  *            *   *
    *  *------------*   *
    *  *------------*   *
    *  *    Text    *   *
    *  *------------*   *
    *-------------------*

So this is my button: 这是我的按钮:

    <ToggleButton
            android:id="@+id/tb"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:layout_margin="18dp"
            android:background="@drawable/custom_toggle_layer"
            android:gravity="bottom | center_horizontal"
            android:textAppearance="@style/CustomTBText"
            android:textColor="@color/white"
            android:textOff="@string/tb_off"
            android:textOn="@string/tb_on" />

The custom background is defined in drawable/custom_toggle_layer.xml : 定制背景在drawable/custom_toggle_layer.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="@drawable/custom_toggle_bg" /> 
        <item android:id="@+android:id/toggle" 
              android:drawable="@drawable/custom_toggle_icon" /> 
    </layer-list>

Where custom_toggle_bg and custom_toggle_icon are both selectors and are working fine. 其中custom_toggle_bgcustom_toggle_icon都是选择器,并且工作正常。

The problem is the text is overlapping with the bottom side of the icon. 问题是文本与图标的底部重叠。 The text is aligned to the bottom, and if I make the icon larger the text sticks to the bottom of the button correctly. 文本与底部对齐,如果我将图标放大,则文本会正确粘贴到按钮的底部。

I first tried to set a padding on the toggle button, but this just moves the text up inside the icon yet a bit more. 我首先尝试在切换按钮上设置填充,但这只是将文本在图标内向上移动了一点。

I'm trying to style the text independently of the button, so I've defined a custom style: 我试图独立于按钮设置文本样式,因此我定义了一种自定义样式:

    <style name="CustomTBText">
        <item name="android:textSize">14sp</item><!-- This works -->
        <item name="android:textColor">@color/white</item><!-- This works -->

        <item name="android:paddingTop">15dp</item><!-- This does not work -->
    </style>

And tried to set a top padding to the text only, but that attribute does not work (the other attributes in the style work fine). 并尝试仅对文本设置顶部填充,但是该属性不起作用(样式中的其他属性可以正常工作)。

How can I fix this? 我怎样才能解决这个问题? I could do a hack setting the text of the button to empty and using an external TextView, but if possible I'd like to do everything using a customized ToggleButton. 我可以通过设置按钮的文本为空并使用外部TextView进行破解,但如果可能的话,我想使用自定义的ToggleButton进行所有操作。

What most tutorials on custom toggle buttons do is to set a layer list as a background to the whole button: 关于自定义切换按钮的大多数教程的作用是将图层列表设置为整个按钮的背景:

<?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="@drawable/custom_toggle_bg" /> 
    <item android:id="@+android:id/toggle" 
          android:drawable="@drawable/custom_toggle_icon" /> 
</layer-list>

This causes problems with the text (and that's also why most tutorials do not show text). 这会导致文本出现问题(这也是大多数教程不显示文本的原因)。 So what I did is to set custom_toggle_bg as background and custom_toggle_icon as drawableTop, and now it works. 因此,我要做的是将custom_toggle_bg设置为背景,并将custom_toggle_icon为drawableTop,现在可以使用了。 This is the button fixed: 这是固定的按钮:

<ToggleButton
        android:id="@+id/tb"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:layout_margin="18dp"
        android:background="@drawable/custom_toggle_bg"
        android:drawableTop="@drawable/custom_toggle_icon"
        android:gravity="bottom | center_horizontal"
        android:textAppearance="@style/CustomTBText"
        android:textColor="@color/white"
        android:textOff="@string/tb_off"
        android:textOn="@string/tb_on" />

You can use the drawable's position, like drawableLeft, drawableTop, drawabeRight, drawableBottom and drawableStart depending on were you want to see the icon. 您可以使用drawable的位置,例如drawableLeft,drawableTop,drawabeRight,drawableBottom和drawableStart,具体取决于您要查看的图标。

Regards! 问候!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM