android seekbar 样式

本文详细介绍了如何自定义SeekBar的样式,包括进度条背景、颜色渐变、圆角设置及使用图片替代的方法。此外还提供了去除SeekBar小球的具体实现步骤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

  1. 本文即收藏,转载比较好的:
  2. 在转载上作下说明:     <item android:id="@android:id/progress"> 
    即是小球走过后的颜色,可以理解为左边的颜色
  3. <item android:id="@android:id/background">
  4.   即是小球即将走的颜色,可以理解为右边的颜色
  5. <corners android:radius="0dip"/>  即是进度条的圆角,0为长方形(90度,直角)





  1. <SeekBar   
  2.          android:id="@+id/player_seekbar"  
  3.          android:layout_width="245px"  
  4.          android:layout_height="25px"  
  5.          android:progressDrawable="@drawable/seekbar_style"  
  6.          android:thumb="@drawable/thumb"  
  7.          android:paddingLeft="16px"  
  8.          android:paddingRight="15px"  
  9.          android:paddingTop="5px"  
  10.          android:paddingBottom="5px"  
  11.          android:progress="0"  
  12.          android:max="0"  
  13.          android:secondaryProgress="0"  
  14.          />    
<SeekBar 
       android:id="@+id/player_seekbar"
       android:layout_width="245px"
       android:layout_height="25px"
       android:progressDrawable="@drawable/seekbar_style"
       android:thumb="@drawable/thumb"
       android:paddingLeft="16px"
       android:paddingRight="15px"
       android:paddingTop="5px"
          android:paddingBottom="5px"
          android:progress="0"
          android:max="0"
          android:secondaryProgress="0"
          />

android:progressDrawable="@drawable/seekbar_style"背景条

seekbar_style配置如下:

Xml代码
  1. <?xmlversion="1.0"encoding="UTF-8"?>  
  2.   
  3. <layer-listxmlns:android="http://schemas.android.com/apk/res/android">  
  4.   
  5.      <itemandroid:id="@android:id/background">  
  6.          <shape>  
  7.              <cornersandroid:radius="5dip"/>  
  8.              <gradient  
  9.                      android:startColor="#ff9d9e9d"  
  10.                      android:centerColor="#ff5a5d5a"  
  11.                      android:centerY="0.75"  
  12.                      android:endColor="#ff747674"  
  13.                      android:angle="270"  
  14.              />  
  15.          </shape>  
  16.      </item>  
  17.   
  18.      <itemandroid:id="@android:id/secondaryProgress">  
  19.          <clip>  
  20.              <shape>  
  21.                  <cornersandroid:radius="5dip"/>  
  22.                  <gradient  
  23.                          android:startColor="#80ffd300"  
  24.                          android:centerColor="#80ffb600"  
  25.                          android:centerY="0.75"  
  26.                          android:endColor="#a0ffcb00"  
  27.                          android:angle="270"  
  28.                  />  
  29.              </shape>  
  30.          </clip>  
  31.      </item>  
  32.   
  33.      <itemandroid:id="@android:id/progress">  
  34.          <clip>  
  35.              <shape>  
  36.                  <cornersandroid:radius="5dip"/>  
  37.                  <gradient  
  38.                          android:startColor="#ff0099CC"  
  39.                          android:centerColor="#ff3399CC"  
  40.                          android:centerY="0.75"  
  41.                          android:endColor="#ff6699CC"  
  42.                          android:angle="270"  
  43.                  />  
  44.              </shape>  
  45.          </clip>  
  46.      </item>  
  47.   
  48. </layer-list>  
<?xml version="1.0" encoding="UTF-8"?>

<layer-list xmlns:android="http://schemas.android.com/apk/res/android">

     <item android:id="@android:id/background">
         <shape>
             <corners android:radius="5dip" />
             <gradient
                     android:startColor="#ff9d9e9d"
                     android:centerColor="#ff5a5d5a"
                     android:centerY="0.75"
                     android:endColor="#ff747674"
                     android:angle="270"
             />
         </shape>
     </item>

     <item android:id="@android:id/secondaryProgress">
         <clip>
             <shape>
                 <corners android:radius="5dip" />
                 <gradient
                         android:startColor="#80ffd300"
                         android:centerColor="#80ffb600"
                         android:centerY="0.75"
                         android:endColor="#a0ffcb00"
                         android:angle="270"
                 />
             </shape>
         </clip>
     </item>

     <item android:id="@android:id/progress">
         <clip>
             <shape>
                 <corners android:radius="5dip" />
                 <gradient
                         android:startColor="#ff0099CC"
                         android:centerColor="#ff3399CC"
                         android:centerY="0.75"
                         android:endColor="#ff6699CC"
                         android:angle="270"
                 />
             </shape>
         </clip>
     </item>

 </layer-list>

或者:用图片如下:

 

Xml代码
  1. <?xmlversion="1.0"encoding="utf-8"?>  
  2. <layer-listxmlns:android="http://schemas.android.com/apk/res/android">  
  3.        
  4.     <itemandroid:id="@android:id/background"   
  5.           android:drawable="@drawable/progress_bg"/>  
  6.         
  7.     <itemandroid:id="@android:id/secondaryProgress"  
  8.           android:drawable="@drawable/second_progress">  
  9.     </item>      
  10.        
  11.     <itemandroid:id="@android:id/progress"  
  12.           android:drawable="@drawable/first_progress">  
  13.            
  14.     </item>   
  15. </layer-list>  
<?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/progress_bg" />
     
 <item android:id="@android:id/secondaryProgress"
       android:drawable="@drawable/second_progress">
 </item> 
 
 <item android:id="@android:id/progress"
       android:drawable="@drawable/first_progress">
     
 </item> 
</layer-list>

方形

Xml代码
  1. <layer-listxmlns:android="http://schemas.android.com/apk/res/android">   
  2.   
  3.   
  4.     <itemandroid:id="@android:id/background"   
  5.     android:drawable="@drawable/progress_bg"/>   
  6.   
  7.   
  8.     <itemandroid:id="@android:id/secondaryProgress">   
  9.      <clipandroid:drawable="@drawable/second_progress"/>   
  10.     </item>   
  11.   
  12.   
  13.     <itemandroid:id="@android:id/progress">   
  14.          <clipandroid:drawable="@drawable/first_progress"/>   
  15.     </item>   
  16.   
  17.   
  18. </layer-list>  
<layer-list xmlns:android="http://schemas.android.com/apk/res/android"> 

    <item android:id="@android:id/background" 
    android:drawable="@drawable/progress_bg" /> 

    <item android:id="@android:id/secondaryProgress"> 
     <clip android:drawable="@drawable/second_progress" /> 
    </item> 

    <item android:id="@android:id/progress"> 
         <clip android:drawable="@drawable/first_progress" /> 
    </item> 

</layer-list>

 

 

 

android:thumb="@drawable/thumb"就是那个会动的球

配置如下:

 

Xml代码
  1. <?xmlversion="1.0"encoding="UTF-8"?>  
  2. <selectorxmlns:android="http://schemas.android.com/apk/res/android">        
  3.        
  4.     <!-- 按下状态-->  
  5.     <item   
  6.         android:state_focused="true"   
  7.         android:state_pressed="true"   
  8.         android:drawable="@drawable/thumb_pressed"/>        
  9.     <!-- 普通无焦点状态 -->  
  10.     <item   
  11.         android:state_focused="false"   
  12.         android:state_pressed="false"  
  13.         android:drawable="@drawable/thumb_normal"/>              
  14.     <!-- 有焦点状态-->  
  15.     <item   
  16.         android:state_focused="true"   
  17.         android:state_pressed="false"              
  18.         android:drawable="@drawable/thumb_focused"/>         
  19.     <!-- 有焦点 -->  
  20.     <item   
  21.         android:state_focused="true"              
  22.         android:drawable="@drawable/thumb_focused"/>     
  23. </selector>








3.去掉THUMB 这个小球方式:

去掉布局中的:

 android:thumb="@drawable/thumb"

<SeekBar
            android:id="@+id/seekbar"
            android:layout_width="fill_parent"
            android:layout_height="4dip"
            android:paddingLeft="16dip"
            android:paddingRight="16dip"
            android:layout_marginTop="4dip"
            android:focusable="false"
            android:focusableInTouchMode="false"
            android:clickable="false"
            android:progressDrawable="@drawable/seekbar"
            >
然后在JAVA代码里面设置:SEEKBAR.setThumb(getThumb());


private ShapeDrawable getThumb() {
        ShapeDrawable thumb = new ShapeDrawable(new RectShape());
        thumb.getPaint().setColor(0x006cff);
        thumb.setIntrinsicHeight(5);
        thumb.setIntrinsicWidth(5);
        return thumb;
    }

 

 

 

demo java code:

 

package com.android.sharefres.five;

import android.app.Activity;
import android.graphics.drawable.ShapeDrawable;
import android.graphics.drawable.shapes.RectShape;
import android.os.Bundle;
import android.widget.SeekBar;

public class SharefresFiveActivity extends Activity {
    /** Called when the activity is first created. */
 private SeekBar mSeekbar;
 public void init(){
  mSeekbar=(SeekBar) findViewById(R.id.seekbar);
  mSeekbar.setThumb(getThumb());
 }
 
 private ShapeDrawable getThumb() {
         ShapeDrawable thumb = new ShapeDrawable(new RectShape());
         thumb.getPaint().setColor(0x006cff);
         thumb.setIntrinsicHeight(5);
         thumb.setIntrinsicWidth(5);
         return thumb;
     }

 

 

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        init();
    }
}

 

 

main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/hello" />
    <SeekBar  android:id="@+id/seekbar" 
            android:layout_width="fill_parent" 
            android:layout_height="4dip" 
            android:paddingLeft="16dip" 
            android:paddingRight="16dip" 
            android:layout_marginTop="4dip" 
            android:focusable="false" 
            android:focusableInTouchMode="false" 
            android:clickable="false" 
            android:progressDrawable="@drawable/seekbar" 
        />

</LinearLayout>

 

seekbar.xml

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android"> 
  
     <item android:id="@android:id/background"> 
         <shape> 
             <corners android:radius="5dip"/> 
             <gradient 
                     android:startColor="#ff9d9e9d" 
                     android:centerColor="#ff5a5d5a" 
                     android:centerY="0.75" 
                     android:endColor="#ff747674" 
                     android:angle="270" 
             /> 
         </shape> 
     </item> 
  
     <item android:id="@android:id/secondaryProgress"> 
         <clip> 
             <shape> 
                 <corners android:radius="5dip"/> 
                 <gradient 
                         android:startColor="#80ffd300" 
                         android:centerColor="#80ffb600" 
                         android:centerY="0.75" 
                         android:endColor="#a0ffcb00" 
                         android:angle="270" 
                 /> 
             </shape> 
         </clip> 
     </item> 
  
     <item android:id="@android:id/progress"> 
         <clip> 
             <shape> 
                 <corners android:radius="5dip"/> 
                 <gradient 
                         android:startColor="#ff0099CC" 
                         android:centerColor="#ff3399CC" 
                         android:centerY="0.75" 
                         android:endColor="#ff6699CC" 
                         android:angle="270" 
                 /> 
             </shape> 
         </clip> 
     </item> 
  
</layer-list> 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值