繁体   English   中英

LinearLayout 底部的按钮

[英]Button in the bottom of LinearLayout

我正在制作一个应用程序,其中有一个RecyclerView 我还添加了一个按钮,但未显示。 见左图。 我现在想要的是制作RecyclerView ,以便可以看到按钮,如右图所示。 我怎样才能做到这一点?

在照片下你可以看到我的 xml 代码。 我希望RecyclerView是相对的/动态的。

在此处输入图像描述

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
    tools:context="com.test.test.MainActivity"
    android:orientation="vertical"
    android:background="@color/colorAccent">

    <include
        android:id="@+id/toolbar"
        layout="@layout/toolbar"/>


    <androidx.cardview.widget.CardView
        android:id="@+id/cardViewMiddle"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginRight="20dp"
        android:layout_marginLeft="20dp"
        android:layout_marginTop="20dp"
        android:layout_marginBottom="20dp"
        app:cardCornerRadius="15dp">

        <androidx.core.widget.NestedScrollView
            android:layout_width="match_parent"
            android:layout_height="match_parent">
            <androidx.recyclerview.widget.RecyclerView

                android:id="@+id/recyclerview"
                android:layout_width="match_parent"
                android:layout_height="match_parent">

            </androidx.recyclerview.widget.RecyclerView>
        </androidx.core.widget.NestedScrollView>
    </androidx.cardview.widget.CardView>


    <Button
        android:id="@+id/btnCheckout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/round_corner"
        android:backgroundTint="@color/colorPrimaryDark"
        android:elevation="16dp"
        android:text="START"
        android:textColor="#FFFFFF"
        android:textStyle="bold" />



</LinearLayout>

问题是您将CardView高度设置为match_parent ,因此它占据了整个屏幕。 最好对这些类型的布局使用 ConstraintLayout,但您也可以像这样以最小的努力修复它:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
    tools:context="com.test.test.MainActivity"
    android:orientation="vertical"
    android:background="@color/colorAccent">

    <include
        android:id="@+id/toolbar"
        layout="@layout/toolbar"/>


    <androidx.cardview.widget.CardView
        android:id="@+id/cardViewMiddle"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_marginRight="20dp"
        android:layout_marginLeft="20dp"
        android:layout_marginTop="20dp"
        android:layout_marginBottom="20dp"
        android:layout_weight="1"
        app:cardCornerRadius="15dp">

        <androidx.core.widget.NestedScrollView
            android:layout_width="match_parent"
            android:layout_height="match_parent">
            <androidx.recyclerview.widget.RecyclerView

                android:id="@+id/recyclerview"
                android:layout_width="match_parent"
                android:layout_height="match_parent">

            </androidx.recyclerview.widget.RecyclerView>
        </androidx.core.widget.NestedScrollView>
    </androidx.cardview.widget.CardView>


    <Button
        android:id="@+id/btnCheckout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/round_corner"
        android:backgroundTint="@color/colorPrimaryDark"
        android:elevation="16dp"
        android:text="START"
        android:textColor="#FFFFFF"
        android:textStyle="bold" />



</LinearLayout>

请注意,我只将 CardView 的layout_height更改为0dp ,然后添加了以下内容: android:layout_weight="1"这将告诉布局尽可能地拉伸(同时不覆盖其下方的其他元素)。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM