【问题标题】:Add checkout button at bottom of page在页面底部添加结帐按钮
【发布时间】:2021-07-18 15:30:26
【问题描述】:

我正在尝试在页面底部的RecyclerView 下方显示一个“结帐”按钮。

我的 XML 文件如下所示:

<androidx.drawerlayout.widget.DrawerLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@android:color/background_light"
    android:id="@+id/drawer_layout"
    tools:context=".Cart">

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

        <include
            layout="@layout/main_toolbar"/>

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="@string/Cart"
            android:gravity="center"
            android:fontFamily="@font/poppinsbold"
            android:textSize="20dp"
            android:textStyle="bold"
            android:layout_marginTop="20dp"
            android:layout_marginLeft="10dp"/>


        <androidx.recyclerview.widget.RecyclerView
            android:id="@+id/recycler_view"
            android:layout_width="match_parent"
            android:layout_height="555dp"
            android:layout_marginBottom="500px"/>
        

        <Button
            android:id="@+id/checkout"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:text="Checkout" />

    </LinearLayout>


    <RelativeLayout

        android:layout_width="300dp"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:background="@android:color/white">


        <include
            layout="@layout/main_nav_drawer"/>
    </RelativeLayout>

</androidx.drawerlayout.widget.DrawerLayout>

不幸的是,当我应用更改时,产品看起来很棒,但按钮没有显示。

我怎样才能让它显示出来?

非常感谢。

【问题讨论】:

    标签: java android xml android-layout layout


    【解决方案1】:

    你需要

    • 删除RecyclerView 的底部填充
    • 不指定RecyclerView的高度,而是使用RecyclerView的权重来展开直到按钮
    • 从Button 中删除android:layout_alignParentBottom="true",因为它与LinearLayout 无关

    应用:

    <?xml version="1.0" encoding="utf-8"?>
    
    <androidx.drawerlayout.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:id="@+id/drawer_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@android:color/background_light">
    
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical">
    
            <include layout="@layout/main_toolbar" />
    
            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginLeft="10dp"
                android:layout_marginTop="20dp"
                android:fontFamily="@font/poppinsbold"
                android:gravity="center"
                android:text="@string/Cart"
                android:textSize="20dp"
                android:textStyle="bold" />
    
    
            <androidx.recyclerview.widget.RecyclerView
                android:id="@+id/recycler_view"
                android:layout_width="match_parent"
                android:layout_weight="1"
                android:layout_height="0dp" />
        
            <Button
                android:id="@+id/checkout"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Checkout" />
    
        </LinearLayout>
    
        <RelativeLayout
            android:layout_width="300dp"
            android:layout_height="match_parent"
            android:layout_gravity="start"
            android:background="@android:color/white">
    
            <include layout="@layout/main_nav_drawer" />
            
        </RelativeLayout>
    
    </androidx.drawerlayout.widget.DrawerLayout>
    

    【讨论】:

    • 非常感谢 Zain 的回答。它实际上解决了我的问题,我会努力使布局完全符合我的要求。
    【解决方案2】:

    问题在于 RecyclerView 的 layout_marginBottom 属性。 500像素?!你在跟我开玩笑吗? 8dp就够了。

    【讨论】:

    • 感谢您的回答。这实际上并没有解决问题,但@Zain 解决了。
    【解决方案3】:

    可能的选择:

    【讨论】:

      猜你喜欢
      • 2011-04-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-10-31
      • 2013-11-18
      • 2021-11-11
      • 1970-01-01
      • 2020-11-12
      相关资源
      最近更新 更多