Android 自定义RatingBar样式

最近项目中用到RatingBar,系统提供的RatingBar 样式太难看了,于是乎自定义了一下RatingBar的样式,个人觉得自定义样式后的效果比系统的好看多了。废话不多说了,直接看效果吧






主界面布局

<LinearLayout 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:orientation="vertical"
    tools:context=".MainActivity" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world" />

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp" >

        <RatingBar
            android:id="@+id/rb_score"
            style="@style/custom_ratingbar_style"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical"
            android:layout_marginLeft="10dp" />

        <TextView
            android:id="@+id/tv_score"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical"
            android:text=""
            android:textColor="@color/red"
            android:textSize="13sp" />
    </LinearLayout>

</LinearLayout>


MainActivity.java

package com.example.customratingbar;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.RatingBar;
import android.widget.TextView;

public class MainActivity extends Activity {

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

	private void findView() {
		RatingBar rb_score = (RatingBar) findViewById(R.id.rb_score);
		TextView tv_score = (TextView) findViewById(R.id.tv_score);
		
		float score = 3.2f;
		
		rb_score.setRating(score);
		tv_score.setText("评分:"+score);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

}



自定义RatingBar样式

<style name="custom_ratingbar_style" parent="@android:style/Widget.RatingBar">  
        <item name="android:progressDrawable">@drawable/custom_ratingbar_progress</item>  
        <item name="android:minHeight">17dip</item>  
        <item name="android:maxHeight">17dip</item>  
        <item name="android:stepSize">0.1</item>
	    <item name="android:isIndicator">true</item>
    </style>  


custom_ratingbar_progress.xml

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >

    <item
        android:id="@+android:id/background"
        android:drawable="@drawable/star_grey">
    </item>
    <item
        android:id="@+android:id/secondaryProgress"
        android:drawable="@drawable/star_grey">
    </item>
    <item
        android:id="@+android:id/progress"
        android:drawable="@drawable/star_red">
    </item>

</layer-list>



下载 工程代码




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值