Android開發(fā)手冊SeekBar拖動條使用實例
??實踐過程
??常用屬性
因為Seekbar繼承自ProgressBar,所以ProgressBar支持的XML屬性SeekBar都適用。
【android:max="100"】:設(shè)置該進度條的最大值
【android:progress="50"】:設(shè)置該進度條的已完成進度值
【android:progressDrawable="@drawable/icon_xinsui"】:自定義drawable顯示
【android:secondaryProgress="50"】:定義二級進度值,值介于0到max。該進度在主進度和背景之間。比如用于網(wǎng)絡(luò)播放視頻時,二級進度用于表示緩沖進度,主進度用于表示播放進度。
【android:splitTrack="false"】:設(shè)置進度條的滑塊圖片
【android:thumb="@drawable/icon_xinsui"】:滑塊底部 背景樣式 (false為透明 )
公共方法總共有14個,小空直接亮個截圖

??基本使用
Java版
<TextView
android:id="@+id/tvProgress"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="數(shù)值范圍0~100之間,當前值:30"
android:textSize="20sp" />
<SeekBar
android:id="@+id/seekBar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:progress="30" />seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
tvProgress.setText("數(shù)值范圍0~100之間,當前值:"+progress);
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
});Kotlin版
seekBar.setOnSeekBarChangeListener(object : OnSeekBarChangeListener {
override fun onProgressChanged(seekBar: SeekBar, progress: Int, fromUser: Boolean) {
tvProgress.setText("數(shù)值范圍0~100之間,當前值:$progress")
}
override fun onStartTrackingTouch(seekBar: SeekBar) {}
override fun onStopTrackingTouch(seekBar: SeekBar) {}
})
然后使用Progressbar的屬性indeterminateDrawable指定即可。
??自定義樣式
這是系統(tǒng)自帶的一個對話框進度條,樣式美觀度不敢恭維。
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_bar);
ProgressDialog pb = new ProgressDialog(this);
pb.setMax(100);
//點擊外部是否可以被取消
pb.setCancelable(true);
//設(shè)置標題
pb.setTitle("下載對話框");
//設(shè)置中間文本內(nèi)容
pb.setMessage("正在下載中....");
pb.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
pb.show();
//在show后調(diào)用
pb.setProgress(50);
}
監(jiān)聽方法 onStartTrackingTouch:當開始滑動滑塊時,會執(zhí)行該方法下的代碼
不管哪個平臺系統(tǒng)樣式都無法滿足多樣的市場需求和審美需求,自定義樣式就是每個平臺都具有的功能。
<SeekBar
android:id="@+id/seekBar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:progress="30"
android:progressDrawable="@drawable/seekbar_one"
android:thumb="@drawable/icon_xinsui" />
<SeekBar
android:id="@+id/seekBarTwo"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:progress="30"
android:progressDrawable="@drawable/seekbar_two"
android:thumb="@drawable/icon_xinsui" />
seekbar_one.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="#33AADD" />
</shape>
</item>
<item android:id="@android:id/secondaryProgress">
<clip>
<shape>
<solid android:color="#3CC4C4" />
</shape>
</clip>
</item>
<item android:id="@android:id/progress">
<clip>
<shape>
<solid android:color="#00ff00" />
</shape>
</clip>
</item>
</layer-list>
seekbar_two.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="5dp"/>
</shape>
<!-- 背景顏色-->
<color android:color="#CCCCCC"/>
</item>
<item android:id="@android:id/progress">
<clip
android:clipOrientation="horizontal"
android:gravity="left">
<shape>
<corners android:radius="5dp"/>
<!-- 開始顏色,中途顏色,最后顏色-->
<gradient
android:startColor="#00FF00"
android:centerColor="#0000FF"
android:endColor="#FF0000"/>
</shape>
</clip>
</item>
</layer-list>
同理thumb其實也是可以自定義的,只不過這個通常美工給個圖就搞定了,如果是動態(tài)整個drawable動畫即可。
除此以外,通常我們還會遇見雙向選擇的滑動條,比如購物類App選擇價格區(qū)間的時候。
可參考:
http://www.dhdzp.com/article/250882.htm
http://www.dhdzp.com/article/250876.htm
以上就是Android開發(fā)手冊SeekBar拖動條使用實例的詳細內(nèi)容,更多關(guān)于Android開發(fā)SeekBar拖動條的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
Android ViewPager相冊橫向移動的實現(xiàn)方法
本篇文章小編為大家介紹,Android ViewPager相冊橫向移動的實現(xiàn)方法。需要的朋友參考下2013-04-04
Android app開發(fā)中的Fragment入門學習教程
這篇文章主要介紹了Android app開發(fā)中的Fragment入門學習教程,包括Fragment的創(chuàng)建和XML布局文件中的Fragment定義等,需要的朋友可以參考下2016-02-02
Android實現(xiàn)自動匹配關(guān)鍵字并且標紅功能
這篇文章主要為大家詳細介紹了Android實現(xiàn)自動匹配關(guān)鍵字并且標紅功能,單關(guān)鍵字和多關(guān)鍵字進行匹配,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-05-05
android TextView中識別多個url并分別點擊跳轉(zhuǎn)方法詳解
在本篇文章里小編給大家整理的是關(guān)于android TextView中識別多個url并分別點擊跳轉(zhuǎn)方法詳解,需要的朋友們可以學習參考下。2019-08-08
Android編程錄音工具類RecorderUtil定義與用法示例
這篇文章主要介紹了Android編程錄音工具類RecorderUtil定義與用法,結(jié)合實例形式分析了Android錄音工具類實現(xiàn)開始錄音、停止錄音、取消錄音、獲取錄音信息等相關(guān)操作技巧,需要的朋友可以參考下2018-01-01

