【问题标题】:how to place ImageViews into RelativeLayout programatically如何以编程方式将图像视图放入RelativeLayout
【发布时间】:2015-02-13 23:31:48
【问题描述】:

我正在将几个 ImageView(鸡蛋)放入 RelativeLayout

a link to image

我想让它们都以相同的大小在行中对齐,但是如果没有足够的空间放置它们,RelativeLayout 只会缩小它们,而所有剩余的都将被省略。

有什么方法可以告诉RelativeLayout 调整它们的大小,使它们都适合屏幕并具有统一的尺寸(与屏幕尺寸和分辨率无关)? 它应该看起来像这样,但是所有这些都包括: (https://docs.google.com/drawings/d/1H1Ij1hyxva2WVxb7HM6jNhju3VLeVn_W-mHhh3VHtJg/edit)

这是我的代码的一部分

private void testLoadEggs() {
    RelativeLayout.LayoutParams params;
    RelativeLayout rl = new RelativeLayout(getContext());

    List<ImageView> imageEggs = new ArrayList();

    for (int i = 0; i < 11; i++) {
        imageEggs.add(new ImageView(getContext()));
        SVG svg = SVGParser.getSVGFromResource(this.getResources(), R.raw.white_egg);
        imageEggs.get(i).setImageDrawable(svg.createPictureDrawable());

        params = new RelativeLayout.LayoutParams(
                LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);

        params.addRule(ALIGN_PARENT_BOTTOM);
        if (i==0)
        {
            params.addRule(ALIGN_PARENT_LEFT);
        }
        else {
            params.addRule(RIGHT_OF,i-1);
        }
        imageEggs.get(i).setId(i);

        imageEggs.get(i).setVisibility(View.VISIBLE);

        rl.addView(imageEggs.get(i), params);

    }

    RelativeLayout.LayoutParams params2 = new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
    params2.addRule(ALIGN_PARENT_BOTTOM);
    params2.addRule(CENTER_HORIZONTAL);
    this.addView(rl, params2);

}`

实际调用:

    this.setBackgroundColor(Color.parseColor("#81D4FA"));
    this.setGravity(Gravity.CENTER);
    testLoadEggs();

【问题讨论】:

    标签: android android-layout imageview android-relativelayout


    【解决方案1】:

    您可以使用orientation="horizo​​ntal" 并将每个布局设置为android:layout_weight="1" 的方式将它们添加到LinearLayout,而不是将视图添加到RelativeLayout

    【讨论】:

      【解决方案2】:

      您应该使用线性布局,您可以轻松地将按钮或图像设置为相同大小。例如;

      <LinearLayout 
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:orientation="horizontal" 
      >
      
      <Button
          android:id="@+id/button2"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:text="Button" 
          android:layout_weight="1"/>
      
      <Button
          android:id="@+id/button1"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:text="Button" 
          android:layout_weight="1"/>
      

      或者您可以使用更多它们并放置您想要的任何东西;

      <LinearLayout 
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:orientation="horizontal" 
      android:weightSum="100"
      >
      
      <Button
          android:id="@+id/button2"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:text="Button" 
          android:layout_weight="30"/>
      
      <Button
          android:id="@+id/button1"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:text="Button" 
          android:layout_weight="30"/>
      <Button
          android:id="@+id/button3"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:text="Button" 
          android:layout_weight="30"/>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-03-01
        • 1970-01-01
        • 1970-01-01
        • 2016-08-16
        相关资源
        最近更新 更多