4 imageViews in the center like 2 lines * 2 rows f

2019-07-19 03:22发布

问题:

i'm trying to put 4 imageViews like they was in 2 lines * 2 rows:

I did this:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/accueil">

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/font"
        android:src="@drawable/fond_menu"
        android:scaleType="centerCrop"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true" />

    <ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent" android:layout_height="fill_parent"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true">

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:paddingBottom="20dp">

            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/topLogo"
                android:layout_marginTop="5dp"
                android:src="@drawable/logo_bleu_petit"
                android:layout_alignParentTop="true"
                android:layout_centerHorizontal="true" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/topText"
                android:lineSpacingMultiplier="1.3"
                android:textSize="16sp"
                android:layout_below="@+id/topLogo"
                android:layout_centerHorizontal="true"
                android:width="260dp"
                android:layout_marginTop="15dp"
                android:gravity="center"
                android:textIsSelectable="false"
                android:layout_marginBottom="10dp" />

            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/firstBlock"
                android:src="@drawable/encart_offres"
                android:layout_alignParentLeft="true"
                android:layout_alignParentStart="true"
                android:layout_below="@+id/topText"
                android:layout_alignParentRight="true"
                android:layout_marginRight="150dp" />

            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/secondBlock"
                android:src="@drawable/encart_contact"
                android:layout_alignParentRight="true"
                android:layout_alignParentEnd="true"
                android:layout_below="@+id/topText"
                android:layout_alignParentBottom="false"
                android:layout_alignParentLeft="true"
                android:layout_alignParentTop="false"
                android:layout_marginLeft="150dp" />

            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/thirdBlock"
                android:src="@drawable/encart_actualites"
                android:layout_alignParentLeft="true"
                android:layout_alignParentStart="true"
                android:layout_below="@+id/firstBlock"
                android:layout_marginTop="40dp"
                android:layout_marginLeft="150dp"
                android:layout_alignParentRight="true" />

            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/fourthBlock"
                android:src="@drawable/encart_mentions"
                android:layout_alignParentRight="true"
                android:layout_alignParentEnd="true"
                android:layout_below="@+id/secondBlock"
                android:layout_marginTop="40dp"
                android:layout_marginRight="150dp"
                android:layout_alignParentLeft="true" />

            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="80dp"
                android:id="@+id/formulaire"
                android:layout_marginTop="15dp"
                android:layout_below="@+id/fourthBlock"
                android:background="@drawable/bouton_formulaire"
                android:layout_centerHorizontal="true" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/bottomText"
                android:lineSpacingMultiplier="1.2"
                android:gravity="center"
                android:textColor="#626262"
                android:textSize="14sp"
                android:layout_centerHorizontal="true"
                android:layout_marginTop="15dp"
                android:width="270dp"
                android:layout_below="@+id/formulaire" />

        </RelativeLayout>
    </ScrollView>

</RelativeLayout>

But with different Device the imageViews are stick between them or are overlap. I think i have to use lines and rows, but i don't know how to di it.

回答1:

Why don't you use TableLayout like this?

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/scroll">
        <TableLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content">

            <TableRow>

                <ImageView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:id="@+id/topLogo1"
                    android:layout_marginTop="5dp"
                    android:src="@drawable/logo_bleu_petit"
                    android:layout_alignParentTop="true"
                    android:layout_centerHorizontal="true" />
            <ImageView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:id="@+id/topLogo2"
                    android:layout_marginTop="5dp"
                    android:src="@drawable/logo_bleu_petit"
                    android:layout_alignParentTop="true"
                    android:layout_centerHorizontal="true" />
            </TableRow>
            <TableRow>

                <ImageView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:id="@+id/topLogo3"
                    android:layout_marginTop="5dp"
                    android:src="@drawable/logo_bleu_petit"
                    android:layout_alignParentTop="true"
                    android:layout_centerHorizontal="true" />
                <ImageView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:id="@+id/topLogo4"
                    android:layout_marginTop="5dp"
                    android:src="@drawable/logo_bleu_petit"
                    android:layout_alignParentTop="true"
                    android:layout_centerHorizontal="true" />
            </TableRow>
        </TableLayout>
</ScrollView>

I hope this can fit your question.



回答2:

try this one

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <ImageView
        android:id="@+id/font"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:scaleType="centerCrop"
        android:src="@drawable/ic_launcher" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center"
        android:orientation="vertical" >

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:orientation="horizontal" >

            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center_horizontal"
                android:scaleType="centerCrop"
                android:src="@drawable/ic_launcher" />

            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center_horizontal"
                android:scaleType="centerCrop"
                android:src="@drawable/ic_launcher" />
        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:orientation="horizontal" >

            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center_horizontal"
                android:scaleType="centerCrop"
                android:src="@drawable/ic_launcher" />

            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center_horizontal"
                android:scaleType="centerCrop"
                android:src="@drawable/ic_launcher" />
        </LinearLayout>
    </LinearLayout>

</LinearLayout>