安卓自定义滑动条

8
我正在尝试实现自定义的SeekBar,就像下面的图片一样
enter image description here

enter image description here

为此,我编写了以下代码
我已经为SeekBar创建了可绘制的进度Drawable,如果需要代码,请告诉我,我会在这里发布。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:id="@+id/llScale"
          xmlns:android="http://schemas.android.com/apk/res/android"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:layout_weight="1"
          android:orientation="vertical"
          android:paddingBottom="5dp">

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@color/white"
    android:orientation="vertical"
    android:padding="5dp">

    <TextView
        android:id="@+id/sliderText"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:padding="5dp"
        android:text="Bernard"
        android:textColor="@color/blue"/>

    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <FrameLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_marginLeft="5dp"
            android:layout_marginRight="5dp"
            android:background="@drawable/slider_background"
            android:padding="3dp">

            <SeekBar
                android:id="@+id/ratingBar"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:max="10"
                android:maxHeight="100dp"
                android:thumbTintMode="multiply"
                android:progressDrawable="@drawable/progressbar"
                android:thumb="@drawable/ic_slider_secondary"/>

            <TextView
                android:id="@+id/seekBarHint"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:gravity="center"
                android:text="Drag slider across"
                android:textColor="@color/dark_gray"/>
        </FrameLayout>

        <TextView
            android:id="@+id/progressText"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@drawable/ic_slider_progress"
            android:gravity="center"
            android:textColor="@color/white"
            android:textStyle="bold"/>
    </FrameLayout>
</LinearLayout>

但是使用上述代码后,我得到的结果如下图所示。浅红色没有被完全填充,留下了一些空间,最后圆圈超出了边界。

enter image description here

enter image description here

Gradle

compileSdkVersion 23
buildToolsVersion "23.0.2"
defaultConfig {
    applicationId "com.myapp"
    minSdkVersion 16
    targetSdkVersion 23
    multiDexEnabled true
    versionCode 1
    versionName "1.0"
    testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}  

有人能帮我解决这个问题吗?


你需要的是一个自定义进度Drawable,你可以通过调用SeekBar#setProgressDrawable(Drawable d)来设置它。 - pskink
@pskink:我已将其设置在XML中android:progressDrawable="@drawable/progressbar"。 - Rajesh Panchal
1个回答

3

我使用了你的代码,并创建了演示,做了以下两个更改:

  1. Set thumbOffset with appropriate value for me 16.5dp worked.

  2. Add one more property android:splitTrack="false" which will help removing which space around thumb drawable.

     <SeekBar
        android:id="@+id/ratingBar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:maxHeight="100dp"
        android:progressDrawable="@drawable/progressbar"
        android:thumb="@drawable/ic_slider_secondary"
        android:splitTrack="false"
        android:thumbOffset="16.5dp"/>  
    

愉快地编码吧 :)


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接