简体   繁体   中英

Android: track invisible for switchcompat when on white background

I have an activity whose background is white, and I added a switchcompat, however when the switch is not active (off) the track is invisible. All that is visible is the thumb so it isn't obvious to the user that it's a switch at all. How can I make the switch's track visible when the switch is in the de-active position?

                    <android.support.v7.widget.SwitchCompat
                        android:id="@+id/someswitchname"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_alignParentRight="true"
                        android:layout_alignParentEnd="true"
                        />

The way to do this is to define a custom theme like this:

<style name="Custom.SwitchCompat.WhiteTrack" parent="Base.Widget.AppCompat.CompoundButton.Switch">
        <!-- Inactive track color(30% transparency) -->
        <item name="android:colorForeground">[some color]</item>
    </style>

then define the theme in your layout like this:

android:theme="@style/Custom.SwitchCompat.WhiteTrack"

et voilà.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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