How to create Custom Ratings bar in Android

前端 未结 13 1229
故里飘歌
故里飘歌 2020-11-22 06:28

Hello all i need to perform Ratings in my application... SO i need to create custom Ratings bar... Can Anyone Help me in this?

13条回答
  •  北海茫月
    2020-11-22 07:18

    Edit

    Have a look at custom rating in motorola http://community.developer.motorola.com/t5/Android-App-Development-for/custom-rating-bar-style-using-android-s-ratingBar-small-style/td-p/10462

    Updated

    styles.xml

    This must be located in your values folder

     
      
        
      
    

    food_rating_bar_full.xml

    This file must be in Drawable folder.

    
    
        
        
        
    
    

    food_ratingbar_full_empty.xml

    This file must be inside Drawable folder.

    
    
    
    
    
    
    
    
    
    
    
    
    
    
    

    food_ratingbar_full_filled.xml

    This file must be located in Drawable folder.

    
    
     
    
    
    
    
    
    
    
    
    
    
    
    

    main.xml file should look like :

    
    
    
        
    
        
    
    

    MainActivity.class should look like :

    import android.app.Activity;
    import android.os.Bundle;
    import android.widget.RatingBar;
    import android.widget.RatingBar.OnRatingBarChangeListener;
    import android.widget.Toast;
    
    public class MainActivity extends Activity {
    /** Called when the activity is first created. */
    
    RatingBar rb;
    
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    
        rb=(RatingBar)findViewById(R.id.ratingBar1);
    
        rb.setOnRatingBarChangeListener(new OnRatingBarChangeListener(){
    
            @Override
            public void onRatingChanged(RatingBar ratingBar, float rating,
                    boolean fromUser) {
                // TODO Auto-generated method stub
                    Toast.makeText(getApplicationContext(),Float.toString(rating),Toast.LENGTH_LONG).show();
    
            }
    
        }); 
    }
    }
    

    I have used two images:

    cookie.jpg

    cookiee.jpg

    This two images are of same size one is used for identifying selected Rating Bar and other for identifying unselected RatingBar

提交回复
热议问题