繁体   English   中英

Android:具有自定义背景的切换按钮的高程

[英]Android : Elevation for toggle button with custom Background

我想使用自定义切换按钮。 目前我使用以下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”不会产生任何影响。

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

嘿使用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"/>

这肯定会起作用

为了解决这个问题,我在切换按钮下面放了一个CardView。 使用卡片视图可以立即设置高程。

感谢您的投入! :-)

暂无
暂无

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

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