Android – SeekBar(I)颜色,颜色设置
<!--默认布局:使用代码设置其颜色-->
<SeekBar
android:id="@+id/bar_r"
android:layout_width="255dp"
android:layout_height="wrap_content"/>
<!--直接设置进度条与滑块颜色-->
<SeekBar
android:id="@+id/bar_g"
android:layout_width="255dp"
android:layout_height="wrap_content"
android:progressTint="@color/colorGreen"
android:thumbTint="@color/colorGreen" />
<!--自定义进度条,滑块样式-->
<SeekBar
android:id="@+id/bar_b"
android:layout_width="255dp"
android:layout_height="wrap_content"
android:maxHeight="2dp"
android:thumb="@drawable/ic_thumb_bike"
android:progressDrawable="@drawable/ic_seekbar_stytle" />
布局内设置:
<!--进度条样式-->
android:progressDrawable="@drawable/seekbar_style"
<!--进度条颜色-->
android:progressTint="@android:color/red"
<!--进度条背景颜色-->
android:progressBackgroundTint="@android:color/white"
<!--滑块样式-->
android:thumb="@drawable/ic_thumb_bicycle"
<!--滑块颜色-->
android:thumbTint="@color/red"
PS: 当同时使用自定义的滑块样式和进度条样式时,若需保持进度条为线状(否则高度将于滑块高度一致),可增加以下属性:
<!--限制进度条高度,不影响滑块大小-->
android:maxHeight="2dp"
seekbar_style.xml(seekbar含三层叠加效果)
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@android:id/background"> <!--最底层:进度条背景颜色-->
<shape>
<corners android:radius="1dp"/>
<solid android:color="@color/colorTY_Little"/>
</shape>
</item>
<item android:id="@android:id/secondaryProgress"> <!--中间层:滑块颜色: 若另设置样式,则被覆盖-->
<clip> <!--剪载图片显示-->
<shape>
<corners android:radius="1dp"/>
<solid android:color="@color/colorTY_Half" />
</shape>
</clip>
</item>
<item android:id="@android:id/progress"> <!--最上层:进度条颜色: 这里使用渐变色-->
<clip>
<shape>
<corners android:radius="1dp"/>
<!--angle:0从左到右,90表示从下到上,数值为45的整数倍;-->
<gradient
android:angle="0"
android:startColor="#4facfe"
android:endColor="#00f2fe"/>
</shape>
</clip>
</item>
</layer-list>
ic_thumb_bike.xml (由 Vector Asset 生成)
<vector android:height="24dp" android:tint="#66CCFF"
android:viewportHeight="24" android:viewportWidth="24"
android:width="24dp" xmlns:android="http://schemas.android.com/apk/res/android">
<path android:fillColor="#ffffff" android:pathData="M20,11c-0.18,0 -0.36,0.03 -0.53,0.05L17.41,9H20V6l-3.72,1.86L13.41,5H9v2h3.59l2,2H11l-4,2L5,9H0v2h4c-2.21,0 -4,1.79 -4,4c0,2.21 1.79,4 4,4c2.21,0 4,-1.79 4,-4l2,2h3l3.49,-6.1l1.01,1.01C16.59,12.64 16,13.75 16,15c0,2.21 1.79,4 4,4c2.21,0 4,-1.79 4,-4C24,12.79 22.21,11 20,11zM4,17c-1.1,0 -2,-0.9 -2,-2c0,-1.1 0.9,-2 2,-2c1.1,0 2,0.9 2,2C6,16.1 5.1,17 4,17zM20,17c-1.1,0 -2,-0.9 -2,-2c0,-1.1 0.9,-2 2,-2s2,0.9 2,2C22,16.1 21.1,17 20,17z"/>
</vector>
Java代码控制:
seekBar.getThumb().setColorFilter(Color.RED, PorterDuff.Mode.SRC_ATOP);//滑块
seekBar.getProgressDrawable().setColorFilter(Color.RED, PorterDuff.Mode.SRC_ATOP);//进度条