Android 向布局中添加多个ImageView

Android 向布局中添加多个ImageView,android,Android,我需要将多个ImageView加载到布局中。图像视图用于使用“星形”图标显示评级。星数(即额定值)在加载期间通过读取配置文件确定 ImageView位于相对的layout布局中 目前,我正在使用类似以下内容: RelativeLayout l = ((RelativeLayout)(findViewById(R.id.RelativeLayout01))); for (int nStarCount = 0; nStarCount < config.getAsInt("stars", 1);

我需要将多个ImageView加载到布局中。图像视图用于使用“星形”图标显示评级。星数(即额定值)在加载期间通过读取配置文件确定

ImageView位于相对的layout布局中

目前,我正在使用类似以下内容:

RelativeLayout l = ((RelativeLayout)(findViewById(R.id.RelativeLayout01)));
for (int nStarCount = 0; nStarCount < config.getAsInt("stars", 1); nStarCount ++) {
    ImageView image = new ImageView(this);
    image .setImageResource(R.drawable.star);
    image .setAdjustViewBounds(true); 
    l.addView(i); 
}
我的问题是:

1不使用xml动态加载是“正确”的方法吗

2如果是,我如何使星星显示在一条线上,目前它们被放置在另一个上面

任何帮助都将不胜感激


RM

1无论哪种方式都可以。不过,大多数人更喜欢XML


2将它们放在线性布局中,方向设置为水平方向。1无论哪种方式都可以。不过,大多数人更喜欢XML


2将它们放在线性布局中,方向设置为水平

对于您正在创建的动态视图,我认为通过代码这样做没有任何错误

但是,如果您更喜欢通过xml进行布局,则可以这样做,然后根据代码调整可见性。即:

<RelativeLayout ...>
   ... stuff goes here
  <LinearLayout ..(setup where you want this
    orienation="horizontal">
     <ImageView id="@+id/star1"
       android:src="@drawable/star"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"/>
     <ImageView id="@+id/star2"
       android:src="@drawable/star"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"/>
     <ImageView id="@+id/star3"
       android:src="@drawable/star"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"/>
     <ImageView id="@+id/star4"
       android:src="@drawable/star"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"/>
     <ImageView id="@+id/star5"
       android:src="@drawable/star"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"/>
  </LinearLayout>
</RelativeLayout>
这段代码实际上看起来有点笨拙,因为您需要按id对每个星图进行寻址

如果您执行了大量这些操作,您可以通过将id保存到列表或其他内容中,使代码不那么难看:

List<Integer> starIds = new List<Integer>();
starIds.add(R.id.star1);
starIds.add(R.id.star2);
...

for (int x=starCount; x<5; x++) {
  ImageView view = (ImageView)findById(starIds[x]);
  view.setVisiblity(GONE);
}

对于您正在创建的动态视图,我认为通过代码进行操作没有任何错误

但是,如果您更喜欢通过xml进行布局,则可以这样做,然后根据代码调整可见性。即:

<RelativeLayout ...>
   ... stuff goes here
  <LinearLayout ..(setup where you want this
    orienation="horizontal">
     <ImageView id="@+id/star1"
       android:src="@drawable/star"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"/>
     <ImageView id="@+id/star2"
       android:src="@drawable/star"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"/>
     <ImageView id="@+id/star3"
       android:src="@drawable/star"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"/>
     <ImageView id="@+id/star4"
       android:src="@drawable/star"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"/>
     <ImageView id="@+id/star5"
       android:src="@drawable/star"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"/>
  </LinearLayout>
</RelativeLayout>
这段代码实际上看起来有点笨拙,因为您需要按id对每个星图进行寻址

如果您执行了大量这些操作,您可以通过将id保存到列表或其他内容中,使代码不那么难看:

List<Integer> starIds = new List<Integer>();
starIds.add(R.id.star1);
starIds.add(R.id.star2);
...

for (int x=starCount; x<5; x++) {
  ImageView view = (ImageView)findById(starIds[x]);
  view.setVisiblity(GONE);
}

如果您知道要分配的最大星星数,则可以使用内置的RatingBar视图。您的XML只会变成这样:

<RatingBar android:id="@+id/ratingbar"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:numStars="5"
    android:stepSize="1.0"/>
在代码中,调用ratingBar.setRating来设置评级。如果你不想看到“空”星星,你可以将最大评级和实际评级设置为相同的数字。通过这种方式,您可以省去重新有效实现同一小部件的麻烦


请参阅:

如果您知道要分配的最大星星数,则可以使用内置的RatingBar视图。您的XML只会变成这样:

<RatingBar android:id="@+id/ratingbar"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:numStars="5"
    android:stepSize="1.0"/>
在代码中,调用ratingBar.setRating来设置评级。如果你不想看到“空”星星,你可以将最大评级和实际评级设置为相同的数字。通过这种方式,您可以省去重新有效实现同一小部件的麻烦


请参阅:

但是如果我不知道会有多少明星,我如何使用XML呢?将它们移动到线性布局是否会破坏相对于其他布局的方向?这就是为什么我使用相对论回答:你没有:。直线布局应该可以。布局的类型只影响其内容,线性在这里很好。你说这两种方式都很好,你能提供一个如何使用xml的示例吗?不,因为我不使用xml或迄今为止还没有使用过xml。但是如果我不知道会有多少星星,我如何使用xml呢?将它们移动到线性布局是否会破坏相对于其他布局的方向?这就是为什么我使用相对论回答:你没有:。直线布局应该可以。布局的类型只影响其内容,线性在这里很好。你说无论哪种方式都很好,你能提供一个如何使用xml的示例吗?不,因为我不使用xml或迄今为止还没有。你在这篇文章中得到了你需要的吗?@Roman M你解决了这个问题吗?我也有同样的问题你在这篇文章中得到了你需要的吗?@Roman M你解决了这个问题吗?我也有同样的问题