How to add the slider (left and right) effect using seekbar?

北城以北 提交于 2019-12-07 07:25:35

Layout

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity">


<SeekBar
    android:id="@+id/seekBar"
    android:layout_width="match_parent"
    android:layout_height="32dp"
    android:layout_alignParentEnd="true"
    android:layout_alignParentRight="true"
    android:layout_alignParentTop="true"
    android:layout_gravity="center"
    android:layout_marginTop="121dp"
    android:max="100"
    android:paddingLeft="30dp"
    android:paddingRight="30dp"
    android:progress="50"
    android:progressDrawable="@drawable/seekbar_progressbar"
    android:thumbTintMode="multiply" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="24dp"
    android:background="@drawable/circle_background_no"
    android:textColor="#dedede"
    android:text="No"
    android:padding="5dp"
    android:layout_marginLeft="17dp"
    android:layout_marginStart="17dp"
    android:layout_alignTop="@+id/textView"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true" />


<TextView
    android:layout_width="wrap_content"
    android:layout_height="24dp"
    android:textColor="#dedede"
    android:background="@drawable/circle_background_yes"
    android:padding="5dp"
    android:paddingRight="7dp"
    android:paddingLeft="7dp"
    android:gravity="center"
    android:text="Yes"
    android:id="@+id/textView"
    android:layout_marginRight="13dp"
    android:layout_marginEnd="13dp"
    android:layout_alignTop="@+id/seekBar"
    android:layout_alignRight="@+id/seekBar"
    android:layout_alignEnd="@+id/seekBar"
    android:layout_marginTop="4dp" />

In drawable

circle_background_yes.xml

 <?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="oval">
    <corners android:radius="10dip"/>
    <stroke android:color="#14ba92" android:width="0dip"/>
    <solid android:color="#14ba92"/>
</shape>

circle_background_no.xml

  <?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="oval">
    <corners android:radius="10dip"/>
    <stroke android:color="#990800" android:width="0dip"/>
    <solid android:color="#990800"/>
</shape>

seekbar_progressbar.xml

<?xml version="1.0" encoding="utf-8"?>

<item>
    <shape android:shape="rectangle" >
        <corners android:radius="5dp"/>

        <gradient
            android:angle="180"
            android:endColor="#14ba92"
            android:startColor="#14ba92" />
    </shape>
</item>
<item>
    <clip>
        <shape android:shape="rectangle" >
            <corners android:radius="5dp" />

            <gradient
                android:angle="180"
                android:endColor="#990800"
                android:startColor="#d14900" />
        </shape>
    </clip>
</item>
<item>
    <clip>
        <shape android:shape="rectangle" >
            <corners android:radius="5dp" />

            <gradient
                android:angle="180"
                android:endColor="#990800"
                android:startColor="#d14900" />
        </shape>
    </clip>
</item>

If you don't like the default thumb, you will have to create your own Drawable, which you can then set the thumb in code with something like:

Drawable thumb = getResources().getDrawable( R.drawable.myThumb );
SeekBar mSeekBar = (SeekBar) findViewById(R.id.mySeekBar);
mSeekbar.setThumb(thumb);

Or you can set the thumb in XML with:

<SeekBar 
    ...
    android:thumb="@drawable/seek_thumb" />

The actual Drawable can be an image, shape, or any other kind of Drawable you could possibly desire. If you want the thumb to change appearance when it is pressed, you will want to create a State List Drawable.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!