简体   繁体   English

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

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

I want to use custom toggle buttons. 我想使用自定义切换按钮。 Currently I use the following xml file to define a "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>

with

and

<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>

as background. 作为背景。

When I include the button in the layout with the following code, it produces a red/green toggle button. 当我使用以下代码在布局中包含按钮时,它会生成一个红色/绿色切换按钮。

<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" />

Now i want this button to have a certain elevation. 现在我希望这个按钮有一定的高度。 Adding android:elevation="2dp" does not produce any effect. 添加android:elevation =“2dp”不会产生任何影响。

Can anyone tell me how to do that? 谁能告诉我怎么做? I was not able to find a solution to that. 我无法找到解决方案。

Hey Use SwitchCompat instead of Toggle button 嘿使用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"/>

This will definatly works 这肯定会起作用

To solve the issue I put a CardView beneath the Toggle button. 为了解决这个问题,我在切换按钮下面放了一个CardView。 Setting an elevation did work right away with the card view. 使用卡片视图可以立即设置高程。

Thanks for the input! 感谢您的投入! :-) :-)

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

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