我的布局中有评级栏小部件,并在布局中设置自定义样式。 我想在我的评级栏中设置更多10颗星,我想在两行中显示评级栏星,就像在单行中它被切断一样。
这是我的布局。
<RatingBar
style="@style/starRatingBar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:numStars="10"
android:stepSize="1" />
感谢任何帮助。
答案 0 :(得分:1)
据我所知,没有办法对单个RatingBar进行换行,所以我建议这样做:
制作两个RatingBars并设置他们的OnRatingBarChangeListeners以便彼此互动。
我写了一个简单的例子来向你展示我的意思:
这将是您的布局文件。我在AS中创建新项目时使用了默认名称。 使用RelativeLayout。
<RelativeLayout 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"
tools:context=".MyActivity">
<RatingBar
android:id="@+id/bar1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:numStars="10"
android:stepSize="1" />
<RatingBar
android:id="@+id/bar2"
android:layout_below="@id/bar1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:numStars="4"
android:stepSize="1" />
</RelativeLayout>
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.RatingBar;
public class MyActivity extends Activity {
private RatingBar ratingBar1;
private RatingBar ratingBar2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my);
setOnChangeListeners();
}
public void setOnChangeListeners(){
ratingBar1 = (RatingBar) findViewById(R.id.bar1);
ratingBar2 = (RatingBar) findViewById(R.id.bar2);
ratingBar1.setOnRatingBarChangeListener(new RatingBar.OnRatingBarChangeListener() {
@Override
public void onRatingChanged(RatingBar ratingBar, float v, boolean b) {
ratingBar2.setRating(0);
ratingBar1.setRating(v);
}
});
ratingBar2.setOnRatingBarChangeListener(new RatingBar.OnRatingBarChangeListener() {
@Override
public void onRatingChanged(RatingBar ratingBar, float v, boolean b) {
ratingBar1.setRating(10);
ratingBar2.setRating(v);
}
});
}
}
如您所见:我们使用辅助函数:setOnChangeListeners()
来初始化ratingBar1
和ratingBar2
。之后我们为两个栏设置onRatingChanged
。对于由10颗星组成的第一个线条,我们确保ratingBar2
设置为0.对于第二个条形,如果更改,ratingBar1
必须设置为10,因此它将被填满。 / p>
试一试!希望它适合你!
答案 1 :(得分:0)
评分栏不能分为两行,因此动态添加评级栏布局
int noOfStarts = 8;
int noOfRatingLine = (noOfStarts/5)+1;
LinearLayout[] linear = new LinearLayout[noOfRatingLine];
RatingBar[] ratingBarNames = new RatingBar[noOfRatingLine];
for(int i =0;i<noOfRatingLine;i++)
{
linear[i] = (LinearLayout)this.getLayoutInflater().inflate(R.layout.app_ratingbar, null);
ratingBarNames[i] = (RatingBar)linear[i].findViewById(R.id.rating);
if(noOfStarts<5){
ratingBarNames[i].setNumStars(noOfStarts);
}
else{
ratingBarNames[i].setNumStars(5);
}
ratingBarNames[i].setStepSize(1);
noOfStarts = noOfStarts-5;
ratingbarLayout.addView(linear[i]);
}
用于inflate的XML布局app_ratingbar.xml
<RatingBar
android:id="@+id/rating"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
</RatingBar>