Sunday, January 13, 2019

Tuỳ chỉnh seekbar android

Khi play nhạc, seekbar cho ta biết mức nhạc đang phát. Seekbar mặc định trông khá đơn điệu.

Ta có thể làm nó đẹp hơn như sau.
Trong thư mục drawable, tạo file xml tên custom.
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="oval">
    <solid android:color="#16BC5C" />
    <stroke
        android:width="1dp"
        android:color="#16BC5C" />
    <size
        android:height="20dp"
        android:width="20dp" />
</shape>
Tạo một file tên border.
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">      
    <item>     
        <shape>
            <corners
                android:radius="5dp" />
            <gradient
                android:angle="270"
                android:startColor="#33000000"
                android:centerColor="#11000000"
                android:endColor="#11000000"
                android:centerY="0.2"
                android:type="linear"
            />         
        </shape>
    </item>
</layer-list>
Tạo một file tên seekbar_progress
<?xml version="1.0" encoding="utf-8"?>
<layer-list    xmlns:android="http://schemas.android.com/apk/res/android" >
    <item
        android:id="@+id/progressshape" >
        <clip>
            <shape
                android:shape="rectangle" >
                <size android:height="5dp"/>
                <corners
                    android:radius="5dp" />
                <solid android:color="#58ACFA"/>       
            </shape>
        </clip>
    </item>
</layer-list>
Tạo thêm một file nữa tên là seekbar_style.
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
    <item
        android:id="@android:id/background"
        android:drawable="@drawable/border" >
    </item>
    <item
        android:id="@android:id/progress" >
        <clip
            android:drawable="@drawable/seekbar_progress" />
    </item>
</layer-list>
Bây giờ tạo file xml của seekbar như sau.
<SeekBar
        android:id="@+id/seekbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:maxHeight="2dp"
        android:minHeight="2dp"
        android:progressDrawable="@drawable/seekbar_style"
        android:thumb="@drawable/custom"
         />
Lúc này seekbar trông như sau.


Bạn có thể tuỳ chỉnh màu theo ý mình.

No comments:

Post a Comment