[Android初级]自定义SeekBar样式(简单说明)

    关于Android的组件SeekBar的使用,其实各个网上的用法也都一样,这里也是差不多的用法,但你看到我的博文,自然也要说清楚怎么个用法啦,接下来,看代码。


一 . 如上图。我们先创建布局,添加组件和他的背景等属性,主要看

android:progressDrawable="@drawable/po_seekbar"
<pre name="code" class="html">android:thumb="@drawable/seekbar_thumb"


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    android:paddingTop="10dp" >

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="自定义样式的SeekBar" />

    <SeekBar
        android:id="@+id/my_seekbar"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:focusable="true"
        android:maxHeight="8dp"
        android:paddingLeft="16dp"
        android:paddingRight="16dp"
        android:progress="50"
        android:progressDrawable="@drawable/po_seekbar"
        android:secondaryProgress="50"
        android:thumb="@drawable/seekbar_thumb" />

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="系统自带的样式" />

    <SeekBar
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" />

    <ProgressBar
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" />

</LinearLayout>

二. 配置拖动条的背景色、进度条的填充色等,po_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>
            <solid android:color="#868F7E" />
        </shape>
    </item>
    <!-- 第二进度条的背景色 -->
    <item android:id="@android:id/secondaryProgress">
        <clip>
            <shape android:shape="rectangle" >
                <solid android:color="#98E3D9" />
            </shape>
        </clip>
    </item>
     <!-- <item android:id="@+android:id/background" android:drawable="@drawable/bar_dn" /> -->
    <!-- 可拖动的进度条颜色 -->
    <item android:id="@android:id/progress">
        <clip>
            <shape>
                <solid android:color="#3DCCB4" />
            </shape>
        </clip>
    </item>
</layer-list>
三.设置 拖动那个大圆点的背景图片,seekbar_thumb.xml:

<?xml version="1.0" encoding="utf-8"?>
<selector
  xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_focused="true" android:state_pressed="false" android:drawable="@drawable/seekbar_thumb_normal" />
    <item android:state_focused="true" android:state_pressed="true" android:drawable="@drawable/seekbar_thumb_pressed" />
    <item android:state_focused="false" android:state_pressed="true" android:drawable="@drawable/seekbar_thumb_pressed" />
    <item android:drawable="@drawable/seekbar_thumb_normal" />
</selector>
四、java代码使用方式如下

private SeekBar mSeekBar;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		mSeekBar = (SeekBar) findViewById(R.id.my_seekbar);
		mSeekBar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
			//当开始被拖动的时候调用
			@Override
			public void onStopTrackingTouch(SeekBar seekBar) {
			}
			//当结束拖动的时候调用
			@Override
			public void onStartTrackingTouch(SeekBar seekBar) {
			}
			//当进度改变的时候调用
			@Override
			public void onProgressChanged(SeekBar seekBar, int progress,
					boolean fromUser) {
				Log.i(TAG, "onProgressChanged->fromUser="+fromUser+",progress="+progress);
				seekBar.setSecondaryProgress(progress);//设置第二进度条的值
			}
		});
	}

最后,为了方便留个demo下载连接,点击如意门

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章