Android開(kāi)發(fā)之自定義星星評(píng)分控件RatingBar用法示例
本文實(shí)例講述了Android開(kāi)發(fā)之自定義星星評(píng)分控件RatingBar用法。分享給大家供大家參考,具體如下:
星級(jí)評(píng)分條RatingBar類似于SeekBar、ProgressBar'等等都可以自定義樣式
它的主要用途就比如淘寶、景點(diǎn) 滿意度等
這里給出兩種自定義效果
如圖所示 第一種是通過(guò)RatingBar獲得分?jǐn)?shù) 第二個(gè)是通過(guò)RatingBar動(dòng)態(tài)調(diào)節(jié)控件屬性(透明度)
由于RatngBar使用簡(jiǎn)單
自定義樣式方法和 http://chabaoo.cn/article/158338.htm一樣
在drawable中建一個(gè)xml文件寫一個(gè) 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>
更多關(guān)于Android相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Android控件用法總結(jié)》、《Android開(kāi)發(fā)入門與進(jìn)階教程》、《Android視圖View技巧總結(jié)》、《Android編程之a(chǎn)ctivity操作技巧總結(jié)》、《Android數(shù)據(jù)庫(kù)操作技巧總結(jié)》及《Android資源操作技巧匯總》
希望本文所述對(duì)大家Android程序設(shè)計(jì)有所幫助。
相關(guān)文章
常見(jiàn)Android編譯優(yōu)化問(wèn)題梳理總結(jié)
這篇文章主要介紹了常見(jiàn)Android編譯優(yōu)化問(wèn)題梳理總結(jié),文章圍繞主題展開(kāi)詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值,需要的小伙伴可以參考一下2022-08-08Android UI實(shí)現(xiàn)SlidingMenu側(cè)滑菜單效果
這篇文章主要為大家詳細(xì)介紹了Android UI實(shí)現(xiàn)SlidingMenu側(cè)滑菜單效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-12-12Android開(kāi)發(fā)中的單例模式應(yīng)用詳解
這篇文章主要介紹了Android開(kāi)發(fā)中的單例模式應(yīng)用,結(jié)合實(shí)例形式詳細(xì)分析了Android開(kāi)發(fā)中常用單例模式的實(shí)現(xiàn)與使用方法,需要的朋友可以參考下2018-01-01Android開(kāi)發(fā)中如何解決Fragment +Viewpager滑動(dòng)頁(yè)面重復(fù)加載的問(wèn)題
這篇文章主要介紹了Android開(kāi)發(fā)中如何解決Fragment +Viewpager滑動(dòng)頁(yè)面重復(fù)加載的問(wèn)題 ,需要的朋友可以參考下2017-07-07Android PraiseTextView實(shí)現(xiàn)朋友圈點(diǎn)贊功能
這篇文章主要為大家詳細(xì)介紹了PraiseTextView簡(jiǎn)單實(shí)現(xiàn)朋友圈點(diǎn)贊功能的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-01-01Android json數(shù)據(jù)解析詳解及實(shí)例代碼
這篇文章主要介紹了 Android json數(shù)據(jù)解析詳解及實(shí)例代碼的相關(guān)資料,需要的朋友可以參考下2017-02-02Android仿iOS側(cè)滑退出當(dāng)前界面功能
這篇文章主要為大家詳細(xì)介紹了Android仿iOS側(cè)滑退出當(dāng)前界面功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-12-12