android - 为什么切换按钮周围有奇怪的填充/边距?

标签 android layout togglebutton android-togglebutton

这是一个奇怪的切换按钮。正如你在图片中看到的,我没有设置任何边距/填充,但它在切换按钮周围只有一个小的 2dp 边距/填充。我想把它和上面的按钮组右对齐,我应该怎么做?

Here is the toggle button

<LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="top|end"
            android:layout_margin="20dp"
            android:clipToPadding="false"
            android:orientation="vertical">

            <co.ceryle.segmentedbutton.SegmentedButtonGroup
                android:id="@+id/dynamic_drawable_group"
                android:layout_width="150dp"
                android:layout_height="30dp"
                android:elevation="2dp"
                app:sbg_animateSelector="fastOutSlowIn"
                app:sbg_animateSelectorDuration="400"
                app:sbg_backgroundColor="@color/white"
                app:sbg_position="0"
                app:sbg_radius="2dp"
                app:sbg_rippleColor="#42bfcc"
                app:sbg_selectorColor="#42bfcc">

                <co.ceryle.segmentedbutton.SegmentedButton
                    android:id="@+id/left_button"
                    android:layout_width="75dp"
                    android:layout_height="30dp"
                    android:layout_weight="1"
                    app:sb_drawableGravity="right"
                    app:sb_drawablePadding="0dp"
                    app:sb_drawableTint="@color/white"
                    app:sb_drawableTint_onSelection="@color/black"
                    app:sb_text="Normal"
                    app:sb_textColor="#42bfcc"
                    app:sb_textColor_onSelection="@color/white"
                    app:sb_textGravity="center"
                    app:sb_textSize="17sp" />

                <co.ceryle.segmentedbutton.SegmentedButton
                    android:id="@+id/right_button"
                    android:layout_width="75dp"
                    android:layout_height="30dp"
                    android:layout_weight="1"
                    app:sb_drawableGravity="right"
                    app:sb_drawableTint="@color/white"
                    app:sb_drawableTint_onSelection="@color/black"
                    app:sb_text="Satellite"
                    app:sb_textColor="#42bfcc"
                    app:sb_textColor_onSelection="@color/white"
                    app:sb_textSize="17sp" />
            </co.ceryle.segmentedbutton.SegmentedButtonGroup>

            <ToggleButton
                android:id="@+id/btn_streetview"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:minEms="0"
                android:minHeight="0dp"
                android:minLines="0"
                android:minWidth="0dp"
                android:textOff="StreetView Off"
                android:textOn="StreetView On"
                android:textSize="17sp"
                android:theme="@style/toggle"
                android:visibility="visible" />


        </LinearLayout>

在代码中我没有添加任何边距或填充。无法弄清楚为什么会这样。

最佳答案

这是 ToggleButton 的默认填充。但是,我设法通过添加 android:background="@null"android:background="@android:color/transparent" 获得此布局。

enter image description here

这是代码:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="top|end"
    android:layout_margin="20dp"
    android:clipToPadding="false"
    android:orientation="vertical">

    <co.ceryle.segmentedbutton.SegmentedButtonGroup
        android:id="@+id/dynamic_drawable_group"
        android:layout_width="150dp"
        android:layout_height="30dp"
        android:elevation="2dp"
        app:sbg_animateSelector="fastOutSlowIn"
        app:sbg_animateSelectorDuration="400"
        app:sbg_backgroundColor="@color/white"
        app:sbg_position="0"
        app:sbg_radius="2dp"
        app:sbg_rippleColor="#42bfcc"
        app:sbg_selectorColor="#42bfcc">

        <co.ceryle.segmentedbutton.SegmentedButton
            android:id="@+id/left_button"
            android:layout_width="75dp"
            android:layout_height="30dp"
            android:layout_weight="1"
            app:sb_drawableGravity="right"
            app:sb_drawablePadding="0dp"
            app:sb_drawableTint="@color/white"
            app:sb_drawableTint_onSelection="@color/black"
            app:sb_text="Normal"
            app:sb_textColor="#42bfcc"
            app:sb_textColor_onSelection="@color/white"
            app:sb_textGravity="center"
            app:sb_textSize="17sp" />

        <co.ceryle.segmentedbutton.SegmentedButton
            android:id="@+id/right_button"
            android:layout_width="75dp"
            android:layout_height="30dp"
            android:layout_weight="1"
            app:sb_drawableGravity="right"
            app:sb_drawableTint="@color/white"
            app:sb_drawableTint_onSelection="@color/black"
            app:sb_text="Satellite"
            app:sb_textColor="#42bfcc"
            app:sb_textColor_onSelection="@color/white"
            app:sb_textSize="17sp" />
    </co.ceryle.segmentedbutton.SegmentedButtonGroup>

    <ToggleButton
        android:id="@+id/btn_streetview"
        android:layout_width="150dp"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:background="@null"
        android:textOff="StreetView Off"
        android:textOn="StreetView On"
        android:textSize="17sp"
        android:theme="@style/toggle"
        android:visibility="visible" />
</LinearLayout>

我认为对代码的这些更改可能会改变您的 ToggleButton 的行为。看看this回答更多。

LE:遵循本教程 here创建自定义 ToggleButton。

关于android - 为什么切换按钮周围有奇怪的填充/边距?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51752301/

相关文章:

android - ToggleButton 示例

android - CustomListView后台问题

layout - 如何在产品 View 页面显示类别自定义设计布局?

Android布局不需要的填充

silverlight - ToggleButton/CheckBox 内容取决于其选中状态?

javascript - 多个 div 崩溃 : close one element if other is open (jQuery)

android - 如何使对话框背景图像不在android中扩展对话框?

android - 从其他应用程序打开已安装的自己的应用程序

java - 一种让我的应用消耗更少内存的方法

android - 在 Android 中,是否可以在代码中向多个收件人发送短信?