我创建了一个自定义的 RatingBar,它可以有 5 种不同的颜色,并且有矩形条,点在星形下方。这是它的样子:
我在几个地方使用这个 View ,在 Android 4.0+ 上一切正常。但是在 Gingerbread 上我有一些问题。这是 View 类的代码:
public class KrinsenRating extends RelativeLayout {
private TextView mRanking;
private RatingBar mStars;
public KrinsenRating(Context context, AttributeSet attrs) {
super(context, attrs);
LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
layoutInflater.inflate(R.layout.krinsen_rating, this);
loadViews();
}
public KrinsenRating(Context context) {
super(context);
loadViews();
}
private void loadViews() {
mRanking = (TextView) findViewById(R.id.ranking_bar_position);
mStars = (RatingBar) findViewById(R.id.ranking_bar);
}
public void setRating(long rating){
RatingConverter rc = new RatingConverter(rating);
Bitmap bmp_empty = ((BitmapDrawable) getResources().getDrawable(R.drawable.star_empty)).getBitmap();
Bitmap bmp_half = null;
Bitmap bmp_full = null;
switch (rc.getColor()) {
case 1:
bmp_half = ((BitmapDrawable) getResources().getDrawable(R.drawable.star_half1)).getBitmap();
bmp_full = ((BitmapDrawable) getResources().getDrawable(R.drawable.star_full1)).getBitmap();
mRanking.setBackgroundColor(getResources().getColor(R.color.ranking1));
break;
case 2:
bmp_half = ((BitmapDrawable) getResources().getDrawable(R.drawable.star_half2)).getBitmap();
bmp_full = ((BitmapDrawable) getResources().getDrawable(R.drawable.star_full2)).getBitmap();
mRanking.setBackgroundColor(getResources().getColor(R.color.ranking2));
break;
case 3:
bmp_half = ((BitmapDrawable) getResources().getDrawable(R.drawable.star_half3)).getBitmap();
bmp_full = ((BitmapDrawable) getResources().getDrawable(R.drawable.star_full3)).getBitmap();
mRanking.setBackgroundColor(getResources().getColor(R.color.ranking3));
break;
case 4:
bmp_half = ((BitmapDrawable) getResources().getDrawable(R.drawable.star_half4)).getBitmap();
bmp_full = ((BitmapDrawable) getResources().getDrawable(R.drawable.star_full4)).getBitmap();
mRanking.setBackgroundColor(getResources().getColor(R.color.ranking4));
break;
case 5:
bmp_half = ((BitmapDrawable) getResources().getDrawable(R.drawable.star_half5)).getBitmap();
bmp_full = ((BitmapDrawable) getResources().getDrawable(R.drawable.star_full5)).getBitmap();
mRanking.setBackgroundColor(getResources().getColor(R.color.ranking5));
break;
}
mStars.setProgressDrawable(GUI.buildRatingBarDrawables(new Bitmap[]{bmp_empty, bmp_half, bmp_full}));
mStars.setRating(rc.getRealRating());
mRanking.setText(rating + "");
}
}
这一行令人困惑:mStars.setProgressDrawable(GUI.buildRatingBarDrawables(new Bitmap[]{bmp_empty, bmp_half, bmp_full})); 但我不得不这样做,因为渲染星星时出现问题在 setProgressDrawable() 调用之后(只显示一个拉伸(stretch)的星形)。这是方法 buildRatingBarDrawables:
public static Drawable buildRatingBarDrawables(Bitmap[] images) {
final int[] requiredIds = { android.R.id.background,
android.R.id.secondaryProgress, android.R.id.progress };
final float[] roundedCorners = new float[] { 5, 5, 5, 5, 5, 5, 5, 5 };
Drawable[] pieces = new Drawable[3];
for (int i = 0; i < 3; i++) {
ShapeDrawable sd = new ShapeDrawable(new RoundRectShape(
roundedCorners, null, null));
BitmapShader bitmapShader = new BitmapShader(images[i],
Shader.TileMode.REPEAT, Shader.TileMode.CLAMP);
sd.getPaint().setShader(bitmapShader);
ClipDrawable cd = new ClipDrawable(sd, Gravity.LEFT,
ClipDrawable.HORIZONTAL);
if (i == 0) {
pieces[i] = sd;
} else {
pieces[i] = cd;
}
}
LayerDrawable ld = new LayerDrawable(pieces);
for (int i = 0; i < 3; i++) {
ld.setId(i, requiredIds[i]);
}
return ld;
}
因此,在 Android ICS 和更新版本上一切正常,但在 Gingerbread 上我遇到了一些奇怪的情况。首先,有时在 Gingerbread 上一切都很好。但往往是根本没有星星。带点的矩形条总是正确的,但星星有时会消失。这是来自 ListView 的示例屏幕,它在每一行中使用评级栏:
如您所见,有时可以,有时不能:/我的导航 slidingMenu 上有这个评级栏,当我慢慢滑动时会显示星星,但在滑动结束后(应该调用 onOpened 时)星星就这样消失了。
你有什么让我更开心的想法吗?:)
我为评级布局添加了 xml 文件:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/avatar_layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center" >
<RatingBar
android:id="@+id/ranking_bar"
android:layout_width="wrap_content"
android:layout_height="30dp"
android:layout_centerHorizontal="true"
android:clickable="false"
android:focusable="false"
android:isIndicator="true"
android:numStars="3"
android:progressDrawable="@drawable/krinsen_rating"
android:stepSize="0.1" />
<TextView
android:id="@+id/ranking_bar_position"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/ranking_bar"
android:layout_centerHorizontal="true"
android:contentDescription="@string/register_avatar"
android:paddingBottom="2dp"
android:paddingLeft="8dp"
android:paddingRight="8dp"
android:paddingTop="2dp"
android:textColor="@android:color/white"
android:textSize="14sp"
android:textStyle="bold" />
</RelativeLayout>
最佳答案
将您的评级可绘制对象定义到图层列表可绘制对象中,然后将其分配给您的 RatingBar 类会是一种更简单的方法。它适用于 android Gingerbread 和之前的版本。
<?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_empty" />
<item android:id="@+android:id/secondaryProgress"
android:drawable="@drawable/star_empty" />
<item android:id="@+android:id/progress"
android:drawable="@drawable/star_full" />
</layer-list>
将其保存到 drawables 文件夹中的 xml 文件中,例如 myCustomRatingBarDrawable.xml
然后在任何 RatingBar View 上:
<RatingBar
android:id="@+id/ratingbar"
android:progressDrawable="@drawable/myCustomRatingBarDrawable"
android:isIndicator="true"
android:max="5"
android:numStars="5"
...
/>
关于android - Gingerbread 设备上的自定义评分栏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18528346/
我正在尝试设置一个puppet节点,但rubygems似乎不正常。如果我通过它自己的二进制文件(/usr/lib/ruby/gems/1.8/gems/facter-1.5.8/bin/facter)在cli上运行facter,它工作正常,但如果我通过由rubygems(/usr/bin/facter)安装的二进制文件,它抛出:/usr/lib/ruby/1.8/facter/uptime.rb:11:undefinedmethod`get_uptime'forFacter::Util::Uptime:Module(NoMethodError)from/usr/lib/ruby
我在我的项目中添加了一个系统来重置用户密码并通过电子邮件将密码发送给他,以防他忘记密码。昨天它运行良好(当我实现它时)。当我今天尝试启动服务器时,出现以下错误。=>BootingWEBrick=>Rails3.2.1applicationstartingindevelopmentonhttp://0.0.0.0:3000=>Callwith-dtodetach=>Ctrl-CtoshutdownserverExiting/Users/vinayshenoy/.rvm/gems/ruby-1.9.3-p0/gems/actionmailer-3.2.1/lib/action_mailer
我想向我的Controller传递一个参数,它是一个简单的复选框,但我不知道如何在模型的form_for中引入它,这是我的观点:{:id=>'go_finance'}do|f|%>Transferirde:para:Entrada:"input",:placeholder=>"Quantofoiganho?"%>Saída:"output",:placeholder=>"Quantofoigasto?"%>Nota:我想做一个额外的复选框,但我该怎么做,模型中没有一个对象,而是一个要检查的对象,以便在Controller中创建一个ifelse,如果没有检查,请帮助我,非常感谢,谢谢
我已经从我的命令行中获得了一切,所以我可以运行rubymyfile并且它可以正常工作。但是当我尝试从sublime中运行它时,我得到了undefinedmethod`require_relative'formain:Object有人知道我的sublime设置中缺少什么吗?我正在使用OSX并安装了rvm。 最佳答案 或者,您可以只使用“require”,它应该可以正常工作。我认为“require_relative”仅适用于ruby1.9+ 关于ruby-主要:Objectwhenrun
我想设置一个默认日期,例如实际日期,我该如何设置?还有如何在组合框中设置默认值顺便问一下,date_field_tag和date_field之间有什么区别? 最佳答案 试试这个:将默认日期作为第二个参数传递。youcorrectlysetthedefaultvalueofcomboboxasshowninyourquestion. 关于ruby-on-rails-date_field_tag,如何设置默认日期?[rails上的ruby],我们在StackOverflow上找到一个类似的问
我将我的Rails应用程序部署到OpenShift,它运行良好,但我无法在生产服务器上运行“Rails控制台”。它给了我这个错误。我该如何解决这个问题?我尝试更新rubygems,但它也给出了权限被拒绝的错误,我也无法做到。railsc错误:Warning:You'reusingRubygems1.8.24withSpring.UpgradetoatleastRubygems2.1.0andrun`gempristine--all`forbetterstartupperformance./opt/rh/ruby193/root/usr/share/rubygems/rubygems
我正在尝试从Postgresql表(table1)中获取数据,该表由另一个相关表(property)的字段(table2)过滤。在纯SQL中,我会这样编写查询:SELECT*FROMtable1JOINtable2USING(table2_id)WHEREtable2.propertyLIKE'query%'这工作正常:scope:my_scope,->(query){includes(:table2).where("table2.property":query)}但我真正需要的是使用LIKE运算符进行过滤,而不是严格相等。然而,这是行不通的:scope:my_scope,->(que
我有一些代码在几个不同的位置之一运行:作为具有调试输出的命令行工具,作为不接受任何输出的更大程序的一部分,以及在Rails环境中。有时我需要根据代码的位置对代码进行细微的更改,我意识到以下样式似乎可行:print"Testingnestedfunctionsdefined\n"CLI=trueifCLIdeftest_printprint"CommandLineVersion\n"endelsedeftest_printprint"ReleaseVersion\n"endendtest_print()这导致:TestingnestedfunctionsdefinedCommandLin
我有一个只接受一个参数的方法:defmy_method(number)end如果使用number调用方法,我该如何引发错误??通常,我如何定义方法参数的条件?比如我想在调用的时候报错:my_method(1) 最佳答案 您可以添加guard在函数的开头,如果参数无效则引发异常。例如:defmy_method(number)failArgumentError,"Inputshouldbegreaterthanorequalto2"ifnumbereputse.messageend#=>Inputshouldbegreaterthano
我使用Ember作为我的前端和GrapeAPI来为我的API提供服务。前端发送类似:{"service"=>{"name"=>"Name","duration"=>"30","user"=>nil,"organization"=>"org","category"=>nil,"description"=>"description","disabled"=>true,"color"=>nil,"availabilities"=>[{"day"=>"Saturday","enabled"=>false,"timeSlots"=>[{"startAt"=>"09:00AM","endAt"=>