如何以编程方式将图像视图放置到RelativeLayout

时间:2015-02-13 23:31:48

标签: android android-layout imageview relativelayout

我将几个ImageViews(鸡蛋)放入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();

2 个答案:

答案 0 :(得分:0)

不是将您的视图添加到RelativeLayout,而是可以将它们添加到LinearLayout,其orientation =“horizo​​ntal”,并且每个布局都设置为android:layout_weight =“1”

答案 1 :(得分:0)

您应该使用线性布局,您可以轻松地将按钮或图像放在相同的尺寸上。例如;

<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"/>

相关问题