Android開發(fā)之自定義星星評分控件RatingBar用法示例
本文實例講述了Android開發(fā)之自定義星星評分控件RatingBar用法。分享給大家供大家參考,具體如下:
星級評分條RatingBar類似于SeekBar、ProgressBar'等等都可以自定義樣式
它的主要用途就比如淘寶、景點 滿意度等
這里給出兩種自定義效果
如圖所示 第一種是通過RatingBar獲得分數(shù) 第二個是通過RatingBar動態(tài)調節(jié)控件屬性(透明度)
由于RatngBar使用簡單
自定義樣式方法和 http://www.dbjr.com.cn/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相關內(nèi)容感興趣的讀者可查看本站專題:《Android控件用法總結》、《Android開發(fā)入門與進階教程》、《Android視圖View技巧總結》、《Android編程之a(chǎn)ctivity操作技巧總結》、《Android數(shù)據(jù)庫操作技巧總結》及《Android資源操作技巧匯總》
希望本文所述對大家Android程序設計有所幫助。
相關文章
Android UI實現(xiàn)SlidingMenu側滑菜單效果
這篇文章主要為大家詳細介紹了Android UI實現(xiàn)SlidingMenu側滑菜單效果,具有一定的參考價值,感興趣的小伙伴們可以參考一下2016-12-12Android開發(fā)中如何解決Fragment +Viewpager滑動頁面重復加載的問題
這篇文章主要介紹了Android開發(fā)中如何解決Fragment +Viewpager滑動頁面重復加載的問題 ,需要的朋友可以參考下2017-07-07Android PraiseTextView實現(xiàn)朋友圈點贊功能
這篇文章主要為大家詳細介紹了PraiseTextView簡單實現(xiàn)朋友圈點贊功能的相關資料,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-01-01Android json數(shù)據(jù)解析詳解及實例代碼
這篇文章主要介紹了 Android json數(shù)據(jù)解析詳解及實例代碼的相關資料,需要的朋友可以參考下2017-02-02