改变的地方是Android动态创建的评价栏中的评价栏中的恒星的颜色 [英] Change the color of the stars in the rating bar where the rating bar is created dynamically in android

查看:114
本文介绍了改变的地方是Android动态创建的评价栏中的评价栏中的恒星的颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我动态地创建我的布局是这样的。我想改变恒星的颜色从默认的蓝色到黄色这个等级吧。

I dynamically create my layout like this. I wanted to change the color of the star in this rating bar from default blue to yellow.

View insertPhoto( int id, int i){
     layout = new LinearLayout(getApplicationContext());
     layout.setLayoutParams(new LayoutParams(180, 250));
     layout.setGravity(Gravity.CENTER);
     layout.setOrientation(1);
     layout.setId(i);
     FrameLayout fl=new FrameLayout(getApplicationContext());
     fl.setLayoutParams(new LayoutParams(116, 116));
     imageView = new ImageView(getApplicationContext());
     imageView.setLayoutParams(new LayoutParams(116,116));
     imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
     imageView.setImageResource(id);
     imageView.setId(i);
     imgoff = new ImageView(getApplicationContext());
     imgoff.setLayoutParams(new LayoutParams(50,116));
     imgoff.setScaleType(ImageView.ScaleType.FIT_END);
     imgoff.setImageResource(getResources().getIdentifier("offer","drawable", getPackageName()));
    // imgoff.setOnClickListener(listener);
     fl.addView(imageView);
     fl.addView(imgoff);
     tv=new TextView(getApplicationContext());
     tv.setLayoutParams(new LayoutParams(116,20));
     tv.setText(namesArr[i]);
     tv.setTextColor(Color.BLACK);
     rb=new RatingBar(getApplicationContext(),null,android.R.attr.ratingBarStyleSmall);
     rb.setLayoutParams(new LayoutParams(80,50));
     rb.setIsIndicator(true);
     rb.setNumStars(5);
     rb.setRating(rateFArr[i]);
     layout.addView(fl);
     layout.addView(tv);
      layout.addView(rb);
     layout.setOnClickListener(listener);
     return layout;

    }

请有人可以帮我解决这个问题。先谢谢了。

Please can someone help me fix this. Thanks in advance.

推荐答案

选项1:

第1步:创建你自己的风格,通过克隆现有样式之一(从$ ANDROID_HOME /平台/ $ SDK /数据/ RES /价值/ styles.xml),将它放在自己的项目的styles.xml,并引用它,当您添加小部件的布局。

Step #1: Create your own style, by cloning one of the existing styles (from $ANDROID_HOME/platforms/$SDK/data/res/values/styles.xml), putting it in your own project's styles.xml, and referencing it when you add the widget to a layout.

第二步:为的RatingBar创建您自己的LayerDrawable XML资源,指向相应的图像使用了吧。原来的样式将指向你,你可以用比较现有的资源。然后,调整你的风格来使用自己的LayerDrawable资源,而不是内置的。

Step #2: Create your own LayerDrawable XML resources for the RatingBar, pointing to appropriate images to use for the bar. The original styles will point you to the existing resources that you can compare with. Then, adjust your style to use your own LayerDrawable resources, rather than built-in ones.

选项2:

您需要做的3星级影像(red_star_full.png,red_star_half.png和red_star_empty.png)和一个XML,仅此而已。

You do need 3 star images (red_star_full.png, red_star_half.png and red_star_empty.png) and one xml, that's all.

在RES /绘把这些拍摄3张照片。

Put these 3 images at res/drawable.

将有以下ratingbar_red.xml:

Put there the following ratingbar_red.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/red_star_empty" />
    <item android:id="@android:id/secondaryProgress" android:drawable="@drawable/red_star_half" />
    <item android:id="@android:id/progress" android:drawable="@drawable/red_star_full" />
</layer-list>

和,最后,告诉你的RatingBar定义中使用此,即

and, finally, tell your ratingbar definition to use this, i.e.

<RatingBar android:progressDrawable="@drawable/ratingbar_red/>

这就是它。

这篇关于改变的地方是Android动态创建的评价栏中的评价栏中的恒星的颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆