RelativeLayout 不适合所有屏幕尺寸 android

RelativeLayout doesn't fit in all screen sizes android

我正在 android 中开发我的第一个应用程序,我被 xml 布局困住了。我在相对布局下的布局中垂直放置了 5 个按钮。我使用的是 sony xperia M,根据我的屏幕尺寸,布局看起来非常好,但对于大屏幕,布局就乱七八糟了。当我 运行 我的应用程序在具有大屏幕尺寸的设备中时,按钮之间没有适当的间隙。我怎样才能使我的布局适合所有屏幕尺寸?

<RelativeLayout 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:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
android:background="@drawable/blue3"
tools:context=".MainActivity">

<Button
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="My Tasks"
    android:id="@+id/mytasks_btn"
    android:onClick="mytask"
    style="@style/btnStyleBeige"
    android:layout_alignParentTop="true"
    android:layout_alignParentStart="true" />

<Button
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="Add Task"
    android:id="@+id/addtask_btn"
    android:layout_marginTop="65dp"
    style="@style/btnStyleBeige"
    android:onClick="addtask"
    android:layout_below="@+id/mytasks_btn"
    android:layout_centerHorizontal="true" />

<Button
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="Show Current Location"
    android:id="@+id/shw_loc_btn"
    style="@style/btnStyleBeige"
    android:onClick="shwlocn"
    android:layout_centerHorizontal="true"
    android:layout_centerVertical="true"
    android:layout_alignParentEnd="true" />

<Button
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="Search Nearby"
    android:id="@+id/button4"
    android:onClick="searchnear"
    style="@style/btnStyleBeige"
    android:layout_below="@+id/shw_loc_btn"
    android:layout_alignParentStart="true"
    android:layout_marginTop="67dp" />

<Button
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    style="@style/btnStyleBeige"
    android:text="Location Distance"
    android:id="@+id/button7"
    android:onClick="locdis"
    android:layout_alignParentBottom="true"
    android:layout_alignParentStart="true" />

您应该考虑将按钮包裹在另一个布局中,例如 LinearLayout。如果你想让你的按钮均匀分布在屏幕上,你应该使用这样的代码。

<?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="match_parent">

    <LinearLayout
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true">

        <Button
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:text="New Button"
            android:id="@+id/button" android:layout_weight="1"/>

        <Button
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:text="New Button"
            android:id="@+id/button2" android:layout_weight="1"/>

        <Button
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:text="New Button"
            android:id="@+id/button3" android:layout_weight="1"/>

        <Button
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:text="New Button"
            android:id="@+id/button4" android:layout_weight="1"/>

        <Button
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:text="New Button"
            android:id="@+id/button5" android:layout_weight="1"/>
    </LinearLayout>
</RelativeLayout>

非常重要的是将 children 的高度设置为 match_parent 并设置适当的 layout_weight 值。