本文實例講述了Android開發之自定義星星評分控件RatingBar用法。分享給大家供大家參考,具體如下:
星級評分條RatingBar類似于SeekBar、ProgressBar'等等都可以自定義樣式
它的主要用途就比如淘寶、景點 滿意度等
這里給出兩種自定義效果

如圖所示 第一種是通過RatingBar獲得分數 第二個是通過RatingBar動態調節控件屬性(透明度)
由于RatngBar使用簡單
自定義樣式方法和 https://www.jb51.net/article/158338.htm一樣
在drawable中建一個xml文件寫一個 layer-list 就行
這里直接給出它的使用方法:
public class MainActivity extends Activity {
RatingBar ratingBar ;RatingBar ratingBar02 ;
TextView textView ;
ImageView imageView ;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ratingBar = (RatingBar) findViewById(R.id.rating);
ratingBar02 = (RatingBar) findViewById(R.id.rating02);
textView = (TextView) findViewById(R.id.textview);
imageView = (ImageView) findViewById(R.id.image);
ratingBar.setOnRatingBarChangeListener(new RatingBar.OnRatingBarChangeListener() {
@Override
public void onRatingChanged(RatingBar ratingBar, float rating, boolean fromUser) {
textView.setText(String.valueOf((int) (rating)));
}
});
ratingBar02.setOnRatingBarChangeListener(new RatingBar.OnRatingBarChangeListener() {
@Override
public void onRatingChanged(RatingBar ratingBar, float rating, boolean fromUser) {
imageView.setAlpha((int)(rating*255/5));
}
});
}
}
然后是布局文件:
文件中的屬性 與ProgressBar一樣
<?xml version="1.0" encoding="utf-8" ?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="@+id/textview"
android:layout_width="match_parent"
android:layout_height="75dp"
android:gravity="center_horizontal"
android:textSize="50dp"/>
<!--android:progressDrawable自定義樣式-->
<RatingBar
android:id="@+id/rating"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:numStars="5"
android:max="255"
android:progress="255"
android:stepSize="0.5"/>
<ImageView
android:id="@+id/image"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:src="@drawable/huangjindiao"/>
<RatingBar
android:id="@+id/rating02"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:numStars="5"
android:progressDrawable="@drawable/my_bar"
android:stepSize="0.5"/>
</LinearLayout>
更多關于Android相關內容感興趣的讀者可查看本站專題:《Android控件用法總結》、《Android開發入門與進階教程》、《Android視圖View技巧總結》、《Android編程之activity操作技巧總結》、《Android數據庫操作技巧總結》及《Android資源操作技巧匯總》
希望本文所述對大家Android程序設計有所幫助。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。