Relative layout Coding [duplicate]

2019-09-21 06:03发布

问题:

Possible Duplicate:
OpenGL and Android relative layout

Hy, I was wondering how to code this xml to code

<RelativeLayout
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:background="@raw/gi"
            android:orientation="vertical" >

            <ImageView
                android:id="@+id/imageView1"
                android:layout_width="64px"
                android:layout_height="64px"
                android:layout_alignParentLeft="true"
                android:layout_alignParentTop="true"
                android:src="@drawable/b1" />

            <ImageView
                android:id="@+id/imageView2"
                android:layout_width="64px"
                android:layout_height="64px"
                android:layout_alignParentTop="true"
                android:layout_toRightOf="@+id/imageView1"
                android:src="@drawable/b2" />

            <ImageView
                android:id="@+id/imageView3"
                android:layout_width="64px"
                android:layout_height="64px"
                android:layout_alignParentTop="true"
                android:layout_toRightOf="@+id/imageView2"
                android:src="@drawable/b3" />

        </RelativeLayout>

as I need it to be placed exactly like that. all I got so far is

gi = new RelativeLayout(this);
        gi.setBackgroundResource(R.raw.gi);

        img1= new ImageView(this);
        img1.setImageResource(R.drawable.b1);
        img1.setLayoutParams();
        img2= new ImageView(this);
        img2.setImageResource(R.drawable.b2);
        img3= new ImageView(this);
        img3.setImageResource(R.drawable.b3);

        gi.addView(img1,64,64);
        gi.addView(img2,64,64);
        gi.addView(img3,64,64);

I'm obviously having some trouble with alignparents which I can't find in the documentation. Also are these pixels, dpi's or something else in the addview

回答1:

check the RelativeLayout.LayoutParams .

it should have all that you need.



回答2:

A RelativeLayout can be useful in most cases, but in this particular one, you may be better served by the following:

<LinearLayout android:orientation="horizontal" android:layout_width="match_parent">
    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="64px"
        android:layout_height="64px"
        android:src="@drawable/b1" />

    <ImageView
        android:id="@+id/imageView2"
        android:layout_width="64px"
        android:layout_height="64px"
        android:src="@drawable/b2" />

    <ImageView
        android:id="@+id/imageView3"
        android:layout_width="64px"
        android:layout_height="64px"
        android:src="@drawable/b3" />
</LinearLayout>

To answer your original question, the reason these are not displaying properly is because of this:

android:layout_toRightOf="@+id/imageView1"

See the @+id? That is creating a new instance of this view, which confuses the XML ids.

What you should be using is this:

android:layout_toRightOf="@id/imageView1"