简体   繁体   中英

can I force android (API8) seekbar to have a specific color?

I'm using android SeekpBar in my android app (API8)

I want to change it's color to be always blue.

but nowadays the color depends on the device model.

is there a way to make it always blue?

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent" 
    android:layout_height="match_parent"
    android:orientation="horizontal"
    android:id="@+id/settingsSeekbarMain">

    <SeekBar
        android:id="@+id/settingsSeekbarBar"
        android:layout_width="match_parent" 
        android:layout_weight="1"
        android:layout_height="wrap_content"  
        android:layout_alignParentRight="true" 
        android:layout_gravity="center_vertical|right" 
        android:layout_marginRight="10dp" 
        android:layout_marginLeft="10dp"
        />
</LinearLayout>

You need to create a custom style for your SeekBar which has a different progress drawable.

Copy the file android-sdk-root\\platforms\\android-8\\data\\res\\drawable\\progress_horizontal.xml into your drawable-folder. Alter the colors as you see fit.

In your styles.xml, create a custom style which uses your custom background drawable:

<style name="CustomSeekbarStyle" parent="@android:style/Widget.SeekBar">
    <item name="android:progressDrawable">@drawable/custom_progress_horizontal</item>
</style>

To use your custom style, either set it explicitly for every SeekBar in your application like this:

<SeekBar
    android:layout_width="match_parent"
    style="@style/CustomSeekbarStyle"
    android:progress="50"
    android:layout_height="wrap_content" />

Or set the style globally in your application theme:

<style name="AppBaseTheme" parent="android:Theme.Light">
    <item name="android:seekBarStyle">@style/CustomSeekbarStyle</item>
</style>

Edit 1: If your application uses newer themes on newer devices (ie holo-theme on ICS devices), you should consider overriding the progress_horizontal file for all relevant API levels. Create multiple drawable-vXX folders (ie drawable-v10 for GB, drawable-v14 for ICS) and put modified progress_horizontal.xml-files in those folders. You'll find those files in your android SDK as well.

Edit 2: This solution however does not come without a somewhat serious flaw: Device vendors like to create custom themes for their android firmwares, often involving different drawables and colors. Using the approach described above, those custom styles are overridden, possibly resulting in an inconsistent user interface.

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