【问题标题】:Adding multiple ImageViews to a layout向布局中添加多个 ImageView
【发布时间】:2010-08-31 15:25:46
【问题描述】:

我需要将几个 ImageView 加载到一个布局中。 ImageViews 用于使用“星”图标显示评级。星数(即评分是在加载过程中通过读取配置文件来确定的。

ImageViews 位于 RelativeLayout 布局内。

目前我正在使用这样的东西:

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

【问题讨论】:

  • 这篇文章有你需要的吗?
  • @Roman M 你解决了这个问题吗?我有同样的问题

标签: android


【解决方案1】:

如果您知道要分配的最大星数,则可以使用内置的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() 来设置评级。如果您不想看到“空”星,您可以将最大评分和实际评分设置为相同的数字。这样,您就可以省去有效地重新实现同一个小部件的麻烦。

见:http://developer.android.com/resources/tutorials/views/hello-formstuff.html#RatingBar

【讨论】:

    【解决方案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 来处理每个星形 imageView。

    如果你做了很多这些,你可以通过将 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);
    }
    

    【讨论】:

      【解决方案3】:

      1) 无论哪种方式都很好。不过大多数人更喜欢 XML。

      2) 将它们放在一个 LinearLayout 中,并将 Orientation 设置为 HORIZONTAL

      【讨论】:

      • 但是如果我不知道那里会有多少颗星,我该如何使用 XML 呢?将它们移动到 LinearLayout 不会破坏相对于其他布局的方向吗? (这就是我使用相对布局的原因)
      • 回答:你没有:)。线性布局应该没问题。布局的类型只影响其内容,线性在这里很好。
      • 你说它“无论哪种方式都很好”,你能提供一个例子如何用 xml 做吗?
      • 不,因为我不使用 XML(或者到目前为止还没有)。
      猜你喜欢
      • 2014-05-29
      • 2018-08-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多