自定义搜索栏android

时间:2017-04-21 04:10:28

标签: android android-custom-view android-seekbar

我正在尝试实现自定义搜索栏,如下图所示 enter image description here

enter image description here

为此我做了下面的代码
我为seekbar progressDrawable创建了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"
}  

任何人都可以帮助我这个

1 个答案:

答案 0 :(得分:3)

我使用了你的代码并创建了demo,其中我做了2次更改,如下所示:

  1. 将thumbOffset设置为适合我16.5dp的值。

  2. 再添加一个属性android:splitTrack =&#34; false&#34;这将有助于删除拇指可绘制的空间。

     <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"/>  
    
  3. 快乐编码:)

相关问题