【问题标题】:Android : Elevation for toggle button with custom BackgroundAndroid:具有自定义背景的切换按钮的高度
【发布时间】:2017-02-06 14:53:30
【问题描述】:

我想使用自定义切换按钮。目前我使用以下 xml 文件来定义“自定义”背景:

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

与

和

<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="#FF0000" />
<corners android:bottomRightRadius="35dp"
    android:bottomLeftRadius="35dp"
    android:topRightRadius="35dp"
    android:topLeftRadius="35dp"/>
</shape>

作为背景。

当我使用以下代码在布局中包含按钮时,它会生成一个红色/绿色切换按钮。

<ToggleButton
    android:id="@+id/ToggleButton1"
    android:layout_height="50dp"
    android:layout_marginRight="30dp"
    android:background="@drawable/toggle_button"
    android:text=""
    android:textOff=""
    android:textOn=""
    android:layout_marginStart="99dp"
    android:layout_marginTop="84dp"
    android:layout_width="50dp"
    android:layout_below="@+id/section_label"
    android:layout_toEndOf="@+id/section_label"
    style="@android:style/Widget.Button.Toggle"
    android:checked="true" />

现在我希望这个按钮有一定的高度。添加 android:elevation="2dp" 不会产生任何效果。

谁能告诉我该怎么做?我无法找到解决方案。

【问题讨论】:

标签: android android-layout togglebutton android-elevation


【解决方案1】:

为了解决这个问题,我在 Toggle 按钮下方放置了一个 CardView。设置海拔确实可以立即使用卡片视图。

感谢您的意见! :-)

【讨论】:

    【解决方案2】:

    嘿使用 SwitchCompat 而不是切换按钮

     <android.support.v7.widget.SwitchCompat
            android:id="@+id/Switch"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textOff=""
            android:text="Toggle Switch"
            android:background="@android:color/transparent"
            android:textOn=""
            android:elevation="5dp"
            android:button="@null"
            android:padding="20dp"/>
    

    这绝对有效

    【讨论】:

    • 嘿,我使用“自定义”背景,当按钮切换时会改变颜色。
    • @Chuchaki 查看我编辑的答案,我希望它会起作用
    • 在SwitchCompat中可以直接使用海拔
    • 嘿!我基本上只需要一个红色圆圈,如果点击它就会变成绿色。 Switch Compat 有一个滑块。如果我应用纯色背景,它仍然不会显示阴影。感谢您的意见!
    猜你喜欢
    • 1970-01-01
    • 2015-12-07
    • 2019-02-13
    • 1970-01-01
    • 2020-05-19
    • 1970-01-01
    • 1970-01-01
    • 2015-09-13
    • 2022-01-23
    相关资源
    最近更新 更多