您是否可以在切换按钮上设置背景颜色而不会遮盖切换按钮?

时间:2014-08-16 23:37:01

标签: android android-layout statelistdrawable android-togglebutton

我有一个ToggleButton。我希望背景清晰,就像一周中几天的默认闹钟应用程序一样。下面的代码涵盖了清晰颜色的切换。有没有办法保持切换和更改背景颜色而不滚动我自己的切换按钮?如果没有,整个过程都会非常糟糕,imo。另外,我真的必须在这里定义一个清晰的颜色,还是我可以在我的xml中使用内置的清晰颜色?

 <ToggleButton
    android:background="@drawable/clear_toggle_button"
    android:id="@+id/btn_sunday"
    android:layout_width="50dp"
    android:layout_height="50dp"
    android:textOn="SUN"
    android:textOff="SUN"
/>

这是我的colors.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <color name="clear">#ffffff00</color>
</resources>

这是drawable文件夹中的颜色状态列表xml。

<?xml version="1.0" encoding="utf-8"?>

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item
        android:state_focused="true"
        android:state_pressed="false"
        android:drawable="@color/clear" />
    <item
        android:state_focused="true"
        android:state_pressed="true"
        android:drawable="@color/clear" />
</selector>

3 个答案:

答案 0 :(得分:10)

  

我希望背景清晰..

透明?是的,android确实有为此定义的颜色。您可以按以下方式访问它:

@android:color/transparent

或者,在代码中:

Color.TRANSPARENT

@android:color/transparentres/values/colors.xml中定义:

<color name="transparent">#00000000</color>

alpha位(第一个和第二个)为零。

但是,您对颜色clear的定义:

<color name="clear">#ffffff00</color>

透明。您可以将transparent显示为任何颜色,并将alpha设置为。在您对clear的定义中,alpha位已完全成为ff - 255 - 不透明。

您的颜色定义产生了这个:

enter image description here

  

有没有办法保持切换并更改背景颜色而不滚动我自己的切换按钮?

事情是:背景颜色和切换是一个可绘制的。整个&#39;&#39;国家由一个单一的drawable代表,'off&#39;州。您不能简单地更改颜色而不会丢失toggle反馈。要更改有关默认ToggleButton's背景的任何内容,您必须为每个州提供备用drawable。

  

我希望背景清晰,就像一周中几天的默认闹钟应用程序一样..

将背景设置为透明将工作。我建议您查看制作ToggleButton所涉及的源代码和资源。例如,on的{​​{1}}和off状态由ToggleButton资源表示。因此,如果您决定更改背景,需要为drawable提供至少两个drawable:每个州一个。

查看默认警报应用程序如何执行此操作。用于ToggleButton的{​​{1}}定义为:

ToggleButtons

drawable days是状态选择器:

<ToggleButton
    android:layout_width="wrap_content"
    android:layout_height="48dp"
    android:layout_gravity="center"
    android:padding="0dp"
    style="@style/body"
    android:textColor="@color/clock_gray"
    **android:background="@drawable/toggle_underline"**
    android:clickable="false"
    android:singleLine="true"/>

如您所见,当toggle_underline设置为<selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:state_pressed="true" android:state_window_focused="true" android:drawable="@drawable/toggle_underline_activated"/> <item android:state_checked="true" android:drawable="@drawable/toggle_underline_activated"/> <item android:drawable="@drawable/toggle_underline_normal"/> </selector> 时(或ToggleButton时),on or checked设置为背景。否则,使用pressed - 状态为@drawable/toggle_underline_activated

@drawable/toggle_underline_normaloff都是9-patch drawables。

toggle_underline_activated.9.png:

enter image description here

toggle_underline_normal.9.png:

enter image description here

您可以在此处获取这些抽奖(以及更多):Link

因此,您可以使用透明背景创建自己的9个补丁drawable,并使用状态选择器drawable - 或者您可以查看默认闹钟项目并使用其toggle_underline_activated文件夹中的drawable

答案 1 :(得分:0)

如果你想改变ToggleButton的颜色而不失去它的“Toggle”,我发现的方法是在你的style.xml上为它创建一个主题:

<style name="AppTheme.Button" parent="Widget.AppCompat.Button.Colored">
    <item name="colorButtonNormal">@color/primary_dark</item>
    <item name="android:textColor">@color/accent</item>
</style>

然后在按钮的xml上设置主题:

    <ToggleButton
            android:id="@+id/part2"
            android:layout_width="60dp"
            android:layout_height="wrap_content"
            android:textOff="B"
            android:textOn="B"
            android:theme="@style/AppTheme.Button"/>

有了这个,我将按钮的颜色设置为我的primary_dark颜色,同时保持它的切换外观。

希望它可以帮到某人。

答案 2 :(得分:-1)

试试这个

<ToggleButton android:background="@color/buttoncolor" android:id="@+id/togglebutton" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textOn="ON" android:textOff="OFF"/>

创建values / color.xml

<color name="buttoncolor">#FF4000</color>

相关问题