標籤:android ui seekbar 拖動條 基礎
Android基礎入門教程——2.3.8 SeekBar(拖動條)
標籤(空格分隔): Android基礎入門教程
本節引言:
本節我們繼續來學習Android的基本UI控制項中的拖動條——SeekBar,相信大家對他並不陌生,最常見的
地方就是音樂播放器或者視頻播放器了,音量控制或者播放進度控制,都用到了這個SeekBar,我們
先來看看SeekBar的類結構,來到官方文檔:SeekBar
嘿嘿,這玩意是ProgressBar的子類耶,也就是ProgressBar的屬性都可以用咯!
而且他還有一個自己的屬性就是:android:thumb,就是允許我們自訂滑塊~
好的,開始本節內容!
1.SeekBar基本用法
好吧,基本用法其實很簡單,常用的屬性無非就下面這幾個常用的屬性,Java代碼裡只要setXxx即可:
android:max=”100” //滑動條的最大值
android:progress=”60” //滑動條的當前值
android:secondaryProgress=”70” //二級滑動條的進度
android:thumb = “@mipmap/sb_icon” //滑塊的drawable
接著要說下SeekBar的事件了,SeekBar.OnSeekBarChangeListener
我們只需重寫三個對應的方法:
onProgressChanged:進度發生改變時會觸發
onStartTrackingTouch:按住SeekBar時會觸發
onStopTrackingTouch:放開SeekBar時觸發
簡單的程式碼範例:
:
實現代碼:
public class MainActivity extends AppCompatActivity { private SeekBar sb_normal; private TextView txt_cur; private Context mContext; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mContext = MainActivity.this; bindViews(); } private void bindViews() { sb_normal = (SeekBar) findViewById(R.id.sb_normal); txt_cur = (TextView) findViewById(R.id.txt_cur); sb_normal.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() { @Override public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) { txt_cur.setText("當前進度值:" + progress + " / 100 "); } @Override public void onStartTrackingTouch(SeekBar seekBar) { Toast.makeText(mContext, "觸碰SeekBar", Toast.LENGTH_SHORT).show(); } @Override public void onStopTrackingTouch(SeekBar seekBar) { Toast.makeText(mContext, "放開SeekBar", Toast.LENGTH_SHORT).show(); } }); }}
2.簡單SeekBar定製:
本來還想著自訂下SeekBar的,後來想想,還是算了,涉及到自訂View的一些東西,可能初學者並
不瞭解,看起來也有點難度,關於自訂View的還是放到進階那裡吧,所以這裡就只是簡單的定製下SeekBar!
定製的內容包括滑塊,以及軌道!
代碼執行個體:
運行:
代碼實現:
1.滑塊狀態Drawable:sb_thumb.xml
<?xml version="1.0" encoding="utf-8"?><selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:state_pressed="true" android:drawable="@mipmap/seekbar_thumb_pressed"/> <item android:state_pressed="false" android:drawable="@mipmap/seekbar_thumb_normal"/></selector>
貼下素材:
2.條形欄Bar的Drawable:sb_bar.xml
這裡用到一個layer-list的drawable資源!其實就是層疊圖片,依次是:背景,二級進度條,當前進度:
<?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="#FFFFD042" /> </shape> </item> <item android:id="@android:id/secondaryProgress"> <clip> <shape> <solid android:color="#FFFFFFFF" /> </shape> </clip> </item> <item android:id="@android:id/progress"> <clip> <shape> <solid android:color="#FF96E85D" /> </shape> </clip> </item></layer-list>
3.然後布局引入SeekBar後,設定下progressDrawable與thumb即可!
<SeekBar android:id="@+id/sb_normal" android:layout_width="match_parent" android:layout_height="wrap_content" android:maxHeight="5.0dp" android:minHeight="5.0dp" android:progressDrawable="@drawable/sb_bar" android:thumb="@drawable/sb_thumb"/>
就是這麼簡單!
本節小結:
好的,關於SeekBar就到這裡,謝謝大家~
著作權聲明:本文為博主原創文章,未經博主允許不得轉載。
Android基礎入門教程——2.3.8 SeekBar(拖動條)