編輯:關於 Android 開發
在Android的眾多基礎控件中,SeekBar(拖動條)與RatingBar(星級評分條)是兩個極具代表性的交互控件。前者常見於音視頻播放的進度調節,後者則廣泛應用於評分打星場景。本文將它們放在一起,從常用屬性、事件監聽到自定義樣式,做一次系統梳理。
拖動條顧名思義就是可以拖動的滑塊,是我們再熟悉不過的交互形式——播放視頻或音樂時拖動進度,背後正是它。它與ProgressBar有許多共通屬性,可以借鑑學習。
常用屬性:
android:max:設置滑動條的最大值;
android:progress:表示當前滑動條的進度值;
android:secondaryProgress:二級滑動條;
android:thumb:設置滑塊的drawable。
這些屬性同樣可以在Java代碼中調用與設置。
SeekBar的監聽機制,與我們在Java SE乃至JavaScript中接觸過的事件模型一脉相承,主要通過OnSeekBarChangeListener的三個回調來響應:
onProgressChanged:進度改變時觸發;
onStartTrackingTouch:手指按下SeekBar時觸發;
onStopTrackingTouch:手指離開SeekBar時觸發。
一個簡單的實例,布局文件如下:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<SeekBar
android:id="@+id/sb1"
android:layout_width="match_parent"
android:layout_height="50dp"
android:max="100"
android:thumb="@mipmap/ic_launcher" />
<TextView
android:id="@+id/tv1"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_marginTop="10dp"
android:gravity="center_vertical"
android:text="Seek的當前值是0"
android:textSize="24sp" />
</LinearLayout>
對應的Java代碼:
public class MainActivity extends Activity {
private TextView textView;
private SeekBar seekBar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
seekBar = (SeekBar) findViewById(R.id.sb1);
textView = (TextView) findViewById(R.id.tv1);
seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override
public void onProgressChanged(SeekBar seekBar, int i, boolean b) {
textView.setText("SeekBar的當前值為" + i);
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
Toast.makeText(MainActivity.this, "你按住了SeekBar", Toast.LENGTH_LONG).show();
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
Toast.makeText(MainActivity.this, "你松開了SeekBar", Toast.LENGTH_LONG).show();
}
});
}
}
自定義SeekBar可分三步走:
第一步,在drawable下新建stateListDrawable,命名為sb_thumb.xml,用於切換滑塊的按下/正常狀態:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="https://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:drawable="@mipmap/press" />
<item android:state_pressed="false" android:drawable="@mipmap/nomal" />
</selector>
第二步,在drawable下新建layer-list,命名為sb_bar.xml,用層疊方式定義背景與進度條:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="https://schemas.android.com/apk/res/android">
<item android:id="@android:id/background">
<shape>
<solid android:color="#FFFFD042" />
</shape>
</item>
<item android:id="@android:id/progress">
<clip>
<shape>
<solid android:color="#FF96E85D" />
</shape>
</clip>
</item>
</layer-list>
第三步,在布局中引用這兩個drawable:
<SeekBar
android:id="@+id/sb1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:max="100"
android:progressDrawable="@drawable/sb_bar"
android:thumb="@drawable/sb_thumb" />
顧名思義,RatingBar呈現的就是我們再熟悉不過的「打星」效果——網購後商家請你給五星好評,背後正是它。
常用屬性:
android:isIndicator:是否作為指示器使用,此時用戶無法手動更改,默認為false;
android:numStars:顯示的星星數量,必須為整數;
android:rating:默認評分值,必須為浮點數;
android:stepSize:每次評分的遞增值,必須為浮點數;
style:樣式可選系統預置的ratingBarStyleSmall或ratingBarStyleIndicator。
事件處理方面,只需為RatingBar設置OnRatingBarChangeListener即可。
最簡單的實例:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<RatingBar
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:rating="3.5" />
</LinearLayout>
自定義RatingBar的思路與SeekBar異曲同工:
第一步,編寫一個layer-list文件ratingbar_full.xml:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="https://schemas.android.com/apk/res/android">
<item android:id="@android:id/background" android:drawable="@mipmap/press" />
<item android:id="@android:id/progress" android:drawable="@mipmap/nomal" />
</layer-list>
第二步,在style文件中引用它並定義完整樣式;
第三步,在布局文件中通過style引用該樣式即可。
掌握SeekBar與RatingBar這對「孿生兄弟」,你就能在進度調節與評分互動等場景中游刃有餘,並透過自定義drawable打造各具特色的視覺效果。
Android Studio 編譯報錯 aapt.exe finished with non-zero exit value 1 排查
Android Studio 編譯報錯:aapt.exe finished with non-zero exit value 1 在 Android Studio 中編譯
Android 自訂 View 入門教學(一)
Android 自訂 View 入門教學(一) 近來在開發一個音樂播放應用,寫到播放介面的 UI 時,就想自己實作一個播放介面。那麼究竟該如何實作自訂 View 呢?透過
Android 屬性動畫入門:ValueAnimator、ObjectAnimator 與組合動畫
Android 屬性動畫入門:ValueAnimator、ObjectAnimator 與組合動畫 Android 動畫體系中,屬性動畫是相當重要的成員。與傳統補間動畫相
Apktool 實戰:修改小米桌面配置並編譯回寫
Apktool 實戰:修改小米桌面配置並編譯回寫 本文示範如何使用 Apktool 反編譯與重新打包 MiuiHome,透過修改桌面配置檔案,自訂小米手機的桌面圖示、文件