Domanda

I have a relative layout with many edittexts and buttons and textviews, and I arranged them in 4 LinearLayouts, as child of a RelativeLayout. The problem is that if I use a different size of screen, the LinearLayouts are not at the same place where I put them, and I can't make it work. How to deal with this problem? This are the codes for the medii.xml file where I have them:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@drawable/mediib" >

    <LinearLayout
        android:id="@+id/linearLayout1"
        android:layout_width="160dp"
        android:layout_height="200dp"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="37dp"
        android:layout_marginTop="255dp"
        android:orientation="vertical" >

        <EditText
            android:id="@+id/editText1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@drawable/nota2"
            android:gravity="center"
            android:hint="@string/nota"
            android:inputType="number"
            android:textColorHint="@color/white" />

        <Button
            android:id="@+id/button2"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@drawable/bb" />

        <Button
            android:id="@+id/button1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@drawable/bbc" />

        <TextView
            android:id="@+id/textView1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:text="TextView"
            android:textColor="@color/white"
            android:textSize="25sp" />
    </LinearLayout>

    <LinearLayout
        android:id="@+id/linearLayout3"
        android:layout_width="160dp"
        android:layout_height="200dp"
        android:layout_alignParentRight="true"
        android:layout_alignTop="@+id/linearLayout1"
        android:layout_marginRight="33dp"
        android:orientation="vertical" >

        <EditText
            android:id="@+id/EditText02"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@drawable/nota2"
            android:gravity="center"
            android:hint="@string/nota"
            android:inputType="number"
            android:textColorHint="@color/white" >

            <requestFocus />
        </EditText>

        <EditText
            android:id="@+id/EditText01"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@drawable/nota2"
            android:gravity="center"
            android:hint="@string/teze"
            android:inputType="number"
            android:textColorHint="@color/white" />

        <Button
            android:id="@+id/Button02"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@drawable/bb" />

        <Button
            android:id="@+id/Button01"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@drawable/bbc" />

        <TextView
            android:id="@+id/TextView01"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:text="TextView"
            android:textColor="@color/white"
            android:textSize="25sp" />
    </LinearLayout>

    <LinearLayout
        android:id="@+id/linearLayout4"
        android:layout_width="160dp"
        android:layout_height="200dp"
        android:layout_alignLeft="@+id/linearLayout3"
        android:layout_alignParentBottom="true"
        android:layout_marginBottom="45dp"
        android:orientation="vertical" >

        <EditText
            android:id="@+id/EditText03"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@drawable/nota2"
            android:gravity="center"
            android:hint="@string/nota"
            android:inputType="number"
            android:textColorHint="@color/white" />

        <Button
            android:id="@+id/Button06"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@drawable/bb" />

        <Button
            android:id="@+id/Button05"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@drawable/bbc" />

        <TextView
            android:id="@+id/TextView03"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:text="TextView"
            android:textColor="@color/white"
            android:textSize="25sp" />
    </LinearLayout>

    <LinearLayout
        android:id="@+id/linearLayout2"
        android:layout_width="160dp"
        android:layout_height="200dp"
        android:layout_alignLeft="@+id/linearLayout1"
        android:layout_alignTop="@+id/linearLayout4"
        android:orientation="vertical" >

        <EditText
            android:id="@+id/EditText04"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@drawable/nota2"
            android:gravity="center"
            android:hint="@string/nota"
            android:inputType="number"
            android:textColorHint="@color/white" />

        <Button
            android:id="@+id/Button04"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@drawable/bb" />

        <Button
            android:id="@+id/Button03"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@drawable/bbc" />

        <TextView
            android:id="@+id/TextView02"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:text="TextView"
            android:textColor="@color/white"
            android:textSize="25sp" />
    </LinearLayout>

</RelativeLayout>

What I want: I want to keep the arrangment I make on all sizes of screens.

How the layout should look:

enter image description here

È stato utile?

Soluzione

Try something like this. My suggestion is to stay away from "magical" margin and padding numbers such as 255 dp and such. Try centering elements

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@drawable/mediib"
    android:orientation="vertical"
    android:weightSum="2" >

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:orientation="horizontal"
        android:weightSum="2" >

        <LinearLayout
            android:id="@+id/linearLayout1"
            android:layout_width="160dp"
            android:layout_height="200dp"
            android:layout_marginLeft="37dp"
            android:layout_marginTop="255dp"
            android:layout_weight="1"
            android:orientation="vertical" >

            <EditText
                android:id="@+id/editText1"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:background="@drawable/nota2"
                android:gravity="center"
                android:hint="@string/nota"
                android:inputType="number"
                android:textColorHint="@color/white" />

            <Button
                android:id="@+id/button2"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:background="@drawable/bb" />

            <Button
                android:id="@+id/button1"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:background="@drawable/bbc" />

            <TextView
                android:id="@+id/textView1"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:gravity="center"
                android:text="TextView"
                android:textColor="@color/white"
                android:textSize="25sp" />
        </LinearLayout>

        <LinearLayout
            android:id="@+id/linearLayout3"
            android:layout_width="160dp"
            android:layout_height="200dp"
            android:layout_marginRight="33dp"
            android:layout_weight="1"
            android:orientation="vertical" >

            <EditText
                android:id="@+id/EditText02"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:background="@drawable/nota2"
                android:gravity="center"
                android:hint="@string/nota"
                android:inputType="number"
                android:textColorHint="@color/white" >

                <requestFocus />
            </EditText>

            <EditText
                android:id="@+id/EditText01"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:background="@drawable/nota2"
                android:gravity="center"
                android:hint="@string/teze"
                android:inputType="number"
                android:textColorHint="@color/white" />

            <Button
                android:id="@+id/Button02"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:background="@drawable/bb" />

            <Button
                android:id="@+id/Button01"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:background="@drawable/bbc" />

            <TextView
                android:id="@+id/TextView01"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:gravity="center"
                android:text="TextView"
                android:textColor="@color/white"
                android:textSize="25sp" />
        </LinearLayout>
    </LinearLayout>

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:orientation="horizontal"
        android:weightSum="2" >

        <LinearLayout
            android:id="@+id/linearLayout4"
            android:layout_width="160dp"
            android:layout_height="200dp"
            android:layout_marginBottom="45dp"
            android:layout_weight="1"
            android:orientation="vertical" >

            <EditText
                android:id="@+id/EditText03"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:background="@drawable/nota2"
                android:gravity="center"
                android:hint="@string/nota"
                android:inputType="number"
                android:textColorHint="@color/white" />

            <Button
                android:id="@+id/Button06"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:background="@drawable/bb" />

            <Button
                android:id="@+id/Button05"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:background="@drawable/bbc" />

            <TextView
                android:id="@+id/TextView03"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:gravity="center"
                android:text="TextView"
                android:textColor="@color/white"
                android:textSize="25sp" />
        </LinearLayout>

        <LinearLayout
            android:id="@+id/linearLayout2"
            android:layout_width="160dp"
            android:layout_height="200dp"
            android:layout_weight="1"
            android:orientation="vertical" >

            <EditText
                android:id="@+id/EditText04"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:background="@drawable/nota2"
                android:gravity="center"
                android:hint="@string/nota"
                android:inputType="number"
                android:textColorHint="@color/white" />

            <Button
                android:id="@+id/Button04"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:background="@drawable/bb" />

            <Button
                android:id="@+id/Button03"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:background="@drawable/bbc" />

            <TextView
                android:id="@+id/TextView02"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:gravity="center"
                android:text="TextView"
                android:textColor="@color/white"
                android:textSize="25sp" />
        </LinearLayout>
    </LinearLayout>

</LinearLayout>

EDIT: I eddited it quite a bit removed the pictures and strings so I can see it in my eclipse you might want to add those back. I got a very similar look to what you are trying to achieve in the picture. Is this what you are trying to achieve?

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    android:weightSum="2" >

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:orientation="horizontal"
        android:weightSum="2" >

        <LinearLayout
            android:id="@+id/linearLayout1"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:gravity="center"
            android:orientation="vertical" >

            <EditText
                android:id="@+id/editText1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:inputType="number"
                android:text="asd" />

            <Button
                android:id="@+id/button2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="asd" />

            <Button
                android:id="@+id/button1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="asd" />

            <TextView
                android:id="@+id/textView1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="TextView"
                android:textSize="25sp" />
        </LinearLayout>

        <LinearLayout
            android:id="@+id/linearLayout3"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:gravity="center"
            android:orientation="vertical" >

            <EditText
                android:id="@+id/EditText02"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:gravity="center"
                android:inputType="number"
                android:text="asd" >
            </EditText>

            <EditText
                android:id="@+id/EditText01"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:inputType="number"
                android:text="asd" />

            <Button
                android:id="@+id/Button02"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="asd" />

            <Button
                android:id="@+id/Button01"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="asd" />

            <TextView
                android:id="@+id/TextView01"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:gravity="center"
                android:text="TextView"
                android:textSize="25sp" />
        </LinearLayout>
    </LinearLayout>

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:orientation="horizontal"
        android:weightSum="2" >

        <LinearLayout
            android:id="@+id/linearLayout4"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:gravity="center"
            android:orientation="vertical" >

            <EditText
                android:id="@+id/EditText03"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:gravity="center"
                android:inputType="number"
                android:text="asd" />

            <Button
                android:id="@+id/Button06"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="asd" />

            <Button
                android:id="@+id/Button05"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="asd" />

            <TextView
                android:id="@+id/TextView03"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:gravity="center"
                android:text="TextView"
                android:textSize="25sp" />
        </LinearLayout>

        <LinearLayout
            android:id="@+id/linearLayout2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:gravity="center"
            android:orientation="vertical" >

            <EditText
                android:id="@+id/EditText04"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:gravity="center"
                android:inputType="number"
                android:text="asd" />

            <Button
                android:id="@+id/Button04"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="asd" />

            <Button
                android:id="@+id/Button03"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="asd" />

            <TextView
                android:id="@+id/TextView02"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:gravity="center"
                android:text="TextView"
                android:textSize="25sp" />
        </LinearLayout>
    </LinearLayout>

</LinearLayout>
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top