自定义RatingBar显示的样式

效果图:

一、自己写一个RatingBar的style

-- 根据自定义星星图片的大小,设置相应的值,否则可能显示不全 -->




二、style用到的资源也需要写一下
ratingbar_drawable.xml

xml version="1.0" encoding="utf-8"?>
xmlns:android="http://schemas.android.com/apk/res/android">
    android:id="@android:id/background"
        android:drawable="@drawable/live_pl_zan0">
    android:id="@android:id/secondaryProgress"
        android:drawable="@drawable/live_pl_zan0">
    android:id="@+android:id/progress"
        android:drawable="@drawable/live_pl_zan1">
注意:一开始,item的id我参照网上例子写,id是的这样
item android:id="@+android:id/background"
结果报错说 Cannot resolve symbol '@+android:id/background' less... (Ctrl+F1)  Validates resource references inside Android XML files.

解释是这样的:http://stackoverflow.com/questions/31662806/cannot-resolve-symbol-error-when-using-androidid-androidid-my-id


If you are creating your own id:

"@+id/your_new_id"

if you are accessing your already created id

"@id/your_old_id"

if you are trying to access Android's system created id

"@android:id/system_id"

you can see the difference, if you are creating your own id then you have to add+. Since you are accessing system ID so you don't have to add +

 意思就是自己新建的id带加号,引用的不需要带!


三、然后就是写布局的时候引用上style

    android:numStars="5" 
    android:rating="3" 
    android:stepSize="0.5" 

activity_pingfen.xml

xml version="1.0" encoding="utf-8"?>
xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
    android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    >

            android:id="@+id/img"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/ic_keyan_companyintro_background"/>

            android:id="@+id/ratingbar"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:numStars="5"
        android:max="255"
        android:progress="255"
        style="@style/roomRatingBar"
        android:layout_below="@id/img"
        android:layout_marginTop="10dp"
        android:stepSize="0.1"/>

            android:id="@+id/textview"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="000"
        android:layout_below="@id/ratingbar"/>


 
四、activity中正常使用即可

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_pingfen);

    RatingBar ratingbar =(RatingBar) findViewById(R.id.ratingbar);
    ratingbar.setRating(0);
    final ImageView img =(ImageView) findViewById(R.id.img);
    img.setAlpha((int)0.0*255/5);
    final TextView textview =(TextView) findViewById(R.id.textview);
    ratingbar.setOnRatingBarChangeListener(new RatingBar.OnRatingBarChangeListener() {
        @Override
        public void onRatingChanged(RatingBar ratingBar, float rating, boolean fromUser) {
            img.setAlpha((int)rating*255/5);
            textview.setText("rating="+rating);
        }
    });
}


你可能感兴趣的:(android)