如何水平设置4个RelativeLayout并根据屏幕大小自动调整

How to set 4 RelativeLayout Horizontally and Adjust automatically according to screen size

请参考屏幕截图。 现在我的问题是 我做了一个 xml 设计,就像屏幕截图中那样。 我有 4 个相对布局包含 imageview 和 textview。 现在我想水平设置这 4 个相对布局,从右、左、开始、结束等 space。 在我的布局中,当它出现在大屏幕上时,开始和结束显示额外的 space,我知道我已经这样设置了。 但是有什么解决办法可以让这些RLs根据屏幕大小自动调整大小吗

 <RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_below="@+id/one"
    android:id="@+id/RL_MainCat"
    android:background="@color/white"
    android:layout_marginTop="10dp">

    <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:layout_marginBottom="10dp"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true">
<RelativeLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/RL_UploadPres"
    android:layout_marginLeft="10dp"
    >
<ImageView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/upload_pres_icon"
    android:id="@+id/icon_uploadpres"
    android:layout_centerHorizontal="true"
    />
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Upload\nPrescription"
        android:textAlignment="center"
        android:textColor="#1f222d"
        android:textSize="11sp"
        android:layout_below="@+id/icon_uploadpres"
        android:layout_marginTop="10dp"
        />
</RelativeLayout>

    <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@+id/RL_UploadPres"
        android:id="@+id/RL_FindPharmacy"
        android:layout_marginLeft="20dp">
        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/find_pharma_icon"
            android:id="@+id/icon_findphar"
            android:layout_centerHorizontal="true"
            />
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text=" Find \n  Pharmacy  "
            android:textAlignment="center"
            android:textColor="#1f222d"
            android:textSize="11sp"
            android:layout_below="@+id/icon_findphar"
            android:layout_marginTop="10dp"
            android:layout_centerHorizontal="true"
            />
    </RelativeLayout>

    <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@+id/RL_FindPharmacy"
        android:id="@+id/RL_BuyOTC"
        android:layout_marginLeft="20dp"
        >
        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/otc_icon"
            android:id="@+id/icon_findotc"
            android:layout_centerHorizontal="true"

            />
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="  Buy \n      OTC     "
            android:textAlignment="center"
            android:textColor="#1f222d"
            android:textSize="11sp"
            android:layout_below="@+id/icon_findotc"
            android:layout_marginTop="10dp"
            android:layout_centerHorizontal="true"/>

    </RelativeLayout>

    <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/RL_BuyMedicine"
        android:layout_toRightOf="@+id/RL_BuyOTC"
        android:layout_marginLeft="20dp"
        android:layout_marginRight="10dp"
        >
        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/medicine_icon"
            android:id="@+id/icon_findmedi"
            android:layout_centerHorizontal="true"
            />
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="  Buy \n Medicines  "
            android:textColor="#1f222d"
            android:textSize="11sp"
            android:textAlignment="center"
            android:layout_marginTop="10dp"
            android:layout_centerHorizontal="true"
            android:layout_below="@+id/icon_findmedi"
            />

    </RelativeLayout>
</RelativeLayout>

在权重和为 4 或 1 的水平方向的线性布局中使用相对布局,并为每个相对布局赋予 1 或 .25 的权重

Inside Relative layout width设置为0dp,这样Layout会在添加weight的时候平分Width

使用 Scalable SP (ssp) and Scalable DP (sdp) 而不是 sp 和 dp,这样尺寸也会自动调整

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:weightSum="1"
    android:orientation="horizontal">

    <RelativeLayout
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight=".25">

    </RelativeLayout>

    <RelativeLayout
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight=".25">

    </RelativeLayout>

    <RelativeLayout
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight=".25">

    </RelativeLayout>

    <RelativeLayout
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight=".25">

    </RelativeLayout>
</LinearLayout>

试试这个:

<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/one"
android:id="@+id/RL_MainCat"
android:background="@color/white"
android:layout_marginTop="10dp">

<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="10dp"
    android:layout_marginBottom="10dp"
    android:layout_centerHorizontal="true"
    android:layout_centerVertical="true">
<RelativeLayout
android:layout_weight="0.25"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:id="@+id/RL_UploadPres"
android:layout_marginLeft="10dp"
>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/upload_pres_icon"
android:id="@+id/icon_uploadpres"
android:layout_centerHorizontal="true"
/>
<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Upload\nPrescription"
    android:textAlignment="center"
    android:textColor="#1f222d"
    android:textSize="11sp"
    android:layout_below="@+id/icon_uploadpres"
    android:layout_marginTop="10dp"
    />
</RelativeLayout>

<RelativeLayout
    android:layout_weight="0.25"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_toRightOf="@+id/RL_UploadPres"
    android:id="@+id/RL_FindPharmacy"
    android:layout_marginLeft="20dp">
    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/find_pharma_icon"
        android:id="@+id/icon_findphar"
        android:layout_centerHorizontal="true"
        />
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text=" Find \n  Pharmacy  "
        android:textAlignment="center"
        android:textColor="#1f222d"
        android:textSize="11sp"
        android:layout_below="@+id/icon_findphar"
        android:layout_marginTop="10dp"
        android:layout_centerHorizontal="true"
        />
</RelativeLayout>

<RelativeLayout
    android:layout_weight="0.25"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_toRightOf="@+id/RL_FindPharmacy"
    android:id="@+id/RL_BuyOTC"
    android:layout_marginLeft="20dp"
    >
    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/otc_icon"
        android:id="@+id/icon_findotc"
        android:layout_centerHorizontal="true"

        />
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="  Buy \n      OTC     "
        android:textAlignment="center"
        android:textColor="#1f222d"
        android:textSize="11sp"
        android:layout_below="@+id/icon_findotc"
        android:layout_marginTop="10dp"
        android:layout_centerHorizontal="true"/>

</RelativeLayout>

<RelativeLayout
    android:layout_weight="0.25"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:id="@+id/RL_BuyMedicine"
    android:layout_toRightOf="@+id/RL_BuyOTC"
    android:layout_marginLeft="20dp"
    android:layout_marginRight="10dp"
    >
    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/medicine_icon"
        android:id="@+id/icon_findmedi"
        android:layout_centerHorizontal="true"
        />
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="  Buy \n Medicines  "
        android:textColor="#1f222d"
        android:textSize="11sp"
        android:textAlignment="center"
        android:layout_marginTop="10dp"
        android:layout_centerHorizontal="true"
        android:layout_below="@+id/icon_findmedi"
        />
</LinearLayout>
</RelativeLayout>

试试这个

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/RL_MainCat"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_below="@+id/one"
    android:layout_marginTop="10dp"
    android:background="#ffffff">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:weightSum="4"
        android:orientation="horizontal">

        <RelativeLayout
            android:id="@+id/RL_UploadPres"
            android:layout_width="match_parent"
            android:layout_weight="1"
            android:layout_height="wrap_content"
            android:layout_marginLeft="10dp">

            <ImageView
                android:id="@+id/icon_uploadpres"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_centerHorizontal="true"
                android:src="@mipmap/ic_launcher" />

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_below="@+id/icon_uploadpres"
                android:layout_marginTop="10dp"
                android:text="Upload\nPrescription"
                android:textAlignment="center"
                android:textColor="#1f222d"
                android:textSize="11sp" />
        </RelativeLayout>

        <RelativeLayout
            android:id="@+id/RL_FindPharmacy"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="20dp"
            android:layout_weight="1"
            android:layout_toRightOf="@+id/RL_UploadPres">

            <ImageView
                android:id="@+id/icon_findphar"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_centerHorizontal="true"
                android:src="@mipmap/ic_launcher" />

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_below="@+id/icon_findphar"
                android:layout_centerHorizontal="true"
                android:layout_marginTop="10dp"
                android:text=" Find \n  Pharmacy  "
                android:textAlignment="center"
                android:textColor="#1f222d"
                android:textSize="11sp" />
        </RelativeLayout>

        <RelativeLayout
            android:id="@+id/RL_BuyOTC"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:layout_marginLeft="20dp"
            android:layout_toRightOf="@+id/RL_FindPharmacy">

            <ImageView
                android:id="@+id/icon_findotc"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_centerHorizontal="true"
                android:src="@mipmap/ic_launcher"

                />

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_below="@+id/icon_findotc"
                android:layout_centerHorizontal="true"
                android:layout_marginTop="10dp"
                android:text="  Buy \n      OTC     "
                android:textAlignment="center"
                android:textColor="#1f222d"
                android:textSize="11sp" />

        </RelativeLayout>

        <RelativeLayout
            android:id="@+id/RL_BuyMedicine"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="20dp"
            android:layout_weight="1"
            android:layout_marginRight="10dp"
            android:layout_toRightOf="@+id/RL_BuyOTC">

            <ImageView
                android:id="@+id/icon_findmedi"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_centerHorizontal="true"
                android:src="@mipmap/ic_launcher" />

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_below="@+id/icon_findmedi"
                android:layout_centerHorizontal="true"
                android:layout_marginTop="10dp"
                android:text="  Buy \n Medicines  "
                android:textAlignment="center"
                android:textColor="#1f222d"
                android:textSize="11sp" />

        </RelativeLayout>

    </LinearLayout>
</RelativeLayout>

低于输出