按钮离开屏幕 android 布局

Button getting out of the screen android layout

我得到了这个相关布局,在 android 工作室的设计屏幕上看起来不错

enter image description here

<?xml version="1.0" encoding="utf-8"?>
<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:padding="8dp"
    tools:context="com.checking.movi.movioperations.CartItems">


    <TextView
        android:id="@+id/txtcartprod"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Productos seleccionados:"
        />

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/recycler_cart"
        android:layout_width="match_parent"
        android:layout_height="649dp"
        android:layout_below="@+id/txtcartprod"
        />

    <Button
        android:id="@+id/btn_placeorder"
        style="?android:attr/buttonStyleSmall"
        android:layout_below="@+id/recycler_cart"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/mybutton"
        android:text="Continuar"
        android:textColor="#ffffff" />

</RelativeLayout>

但是当我在 phone 上测试时,如果我减小 recyclerview 的大小,按钮就会离开屏幕它将 3 保持在同一屏幕上

以 dp(或像素)为单位为 views 提供宽度和高度是不正确的方法。因为所有设备的屏幕尺寸都不相同。 Android Studio 中的设计选项卡以 Pixel phone 显示您的布局,其高度大于您的手机 phone。因此,您在手机中看不到该按钮 phone。

我在你的按钮上添加了一行又删除了另一行

添加了这个:

android:layout_alignParentBottom="true"

删除了这个:

android:layout_below="@+id/txtcartprod"

RecyclerView 的高度更改为 match_parent(使其取决于屏幕尺寸):

android:layout_height="match_parent"

并添加了这一行:

android:layout_above= "@id/btn_placeorder"

最终版本:

<?xml version="1.0" encoding="utf-8"?>
<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:padding="8dp"
    tools:context="com.checking.movi.movioperations.CartItems">


    <TextView
        android:id="@+id/txtcartprod"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Productos seleccionados:"
        />

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/recycler_cart"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_above= "@id/btn_placeorder"
        android:layout_below="@+id/txtcartprod"
        />

    <Button
        android:id="@+id/btn_placeorder"
        style="?android:attr/buttonStyleSmall"
        android:layout_alignParentBottom="true"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/mybutton"
        android:text="Continuar"
        android:textColor="#ffffff" />

</RelativeLayout>

希望您理解我所做的所有更改。如果有困难,欢迎在评论中提问。