布局权重分布与 XML 预览中所示不同

layout weight distribution is not as shown in XML preview

我有以下 XML 布局 -

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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="@color/white"
    tools:context=".activities.ShoppingCartActivity">

    <LinearLayout
        android:id="@+id/activity_shopping_cart_main_viewgroup"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <RelativeLayout
            android:id="@+id/activity_shopping_cart_top_view_linear_layout"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="0.1"
            android:orientation="horizontal"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent">

            <TextView
                android:id="@+id/activity_shopping_cart_title"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerVertical="true"
                android:layout_marginStart="@dimen/marketplace_14dp"
                android:text="@string/activity_shopping_cart_shopping_cart"
                android:textSize="20sp"
                android:textStyle="bold" />

            <ImageView
                android:layout_width="30dp"
                android:layout_height="30dp"
                android:layout_alignParentEnd="true"
                android:layout_centerVertical="true"
                android:layout_marginEnd="@dimen/marketplace_14dp"
                android:src="@drawable/close_small3x" />

        </RelativeLayout>


        <LinearLayout
            android:id="@+id/activity_shopping_cart_rv_viewgroup"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="0.45"
            android:orientation="vertical">

            <View
                android:id="@+id/activity_shopping_cart_top_view"
                android:layout_width="match_parent"
                android:layout_height="1dp"
                android:background="@color/very_light_grey"
                app:layout_constraintTop_toBottomOf="@+id/activity_shopping_cart_rv_viewgroup" />


            <ProgressBar
                android:id="@+id/activity_shopping_cart_progress_bar"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginStart="@dimen/marketplace_14dp"
                android:layout_marginTop="@dimen/marketplace_15dp"
                android:layout_marginEnd="@dimen/marketplace_14dp"
                app:layout_constraintTop_toBottomOf="@+id/activity_shopping_cart_top_view" />

            <androidx.recyclerview.widget.RecyclerView
                android:id="@+id/activity_shopping_cart_products_rv"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginStart="@dimen/marketplace_14dp"
                android:layout_marginTop="@dimen/marketplace_15dp"
                android:layout_marginEnd="@dimen/marketplace_14dp"
                app:layout_constraintTop_toBottomOf="@+id/activity_shopping_cart_top_view"
                tools:itemCount="4"
                tools:listitem="@layout/marketplace_cart_holder" />


        </LinearLayout>

        <LinearLayout
            android:id="@+id/activity_shopping_cart_products_summary_viewgroup"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_marginStart="@dimen/marketplace_14dp"
            android:layout_marginTop="@dimen/marketplace_15dp"
            android:layout_marginEnd="@dimen/marketplace_14dp"
            android:layout_marginBottom="@dimen/marketplace_15dp"
            android:layout_weight="0.45"
            android:orientation="vertical"
            app:layout_constraintTop_toBottomOf="@+id/activity_shopping_cart_rv_viewgroup">

            <View
                android:id="@+id/activity_shopping_cart_bottom_view"
                android:layout_width="match_parent"
                android:layout_height="1dp"
                android:layout_marginTop="@dimen/marketplace_15dp"
                android:background="@color/very_light_grey" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginTop="@dimen/marketplace_15dp"
                android:text="@string/activity_shopping_cart_tax_0"
                android:textSize="18sp"
                android:textStyle="bold" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/activity_shopping_cart_tax_0"
                android:textSize="18sp"
                android:textStyle="bold" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginTop="@dimen/marketplace_15dp"
                android:text="@string/activity_shopping_cart_all_amounts_are_in_usd"
                android:textSize="16sp" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/activity_shopping_cart_shipping_fees_calculated_at_checkout"
                android:textSize="16sp" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginTop="@dimen/marketplace_15dp"
                android:text="@string/activity_shopping_cart_terms_and_policies"
                android:textSize="12sp"
                android:textStyle="bold" />

            <Button
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="@dimen/marketplace_15dp"
                android:background="@color/color_black"
                android:text="@string/activity_shopping_cart_proceed_to_checkout" />

            <Button
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="@dimen/marketplace_15dp"
                android:background="@drawable/shopping_cart__clear_background_button"
                android:textColor="@color/color_black"
                android:text="@string/activity_shopping_cart_continue_shopping" />


        </LinearLayout>

    </LinearLayout>


</androidx.constraintlayout.widget.ConstraintLayout>

在预览中看起来像这样 -

我面临的问题是,出于某种原因,底部按钮 "continue shopping" 被缩小了 -

即使我向下复制同一个按钮并替换为当前按钮,复制的按钮也会缩小到这个大小。

这是我的 clear_background_button.xml 文件 -

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">

    <stroke
        android:width="1dp"
        android:color="@color/red_delete" />
    <solid android:color="@color/white" />


</shape>

缩水的原因是什么?

试试这个

<?xml version="1.0" encoding="utf-8"?>
<ScrollView
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:fillViewport="true">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/white"
tools:context=".activities.ShoppingCartActivity">

<LinearLayout
    android:id="@+id/activity_shopping_cart_main_viewgroup"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <RelativeLayout
        android:id="@+id/activity_shopping_cart_top_view_linear_layout"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="0.1"
        android:orientation="horizontal"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <TextView
            android:id="@+id/activity_shopping_cart_title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerVertical="true"
            android:layout_marginStart="@dimen/marketplace_14dp"
            android:text="@string/activity_shopping_cart_shopping_cart"
            android:textSize="20sp"
            android:textStyle="bold" />

        <ImageView
            android:layout_width="30dp"
            android:layout_height="30dp"
            android:layout_alignParentEnd="true"
            android:layout_centerVertical="true"
            android:layout_marginEnd="@dimen/marketplace_14dp"
            android:src="@drawable/close_small3x" />

    </RelativeLayout>


    <LinearLayout
        android:id="@+id/activity_shopping_cart_rv_viewgroup"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="0.45"
        android:orientation="vertical">

        <View
            android:id="@+id/activity_shopping_cart_top_view"
            android:layout_width="match_parent"
            android:layout_height="1dp"
            android:background="@color/very_light_grey"
            app:layout_constraintTop_toBottomOf="@+id/activity_shopping_cart_rv_viewgroup" />


        <ProgressBar
            android:id="@+id/activity_shopping_cart_progress_bar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginStart="@dimen/marketplace_14dp"
            android:layout_marginTop="@dimen/marketplace_15dp"
            android:layout_marginEnd="@dimen/marketplace_14dp"
            app:layout_constraintTop_toBottomOf="@+id/activity_shopping_cart_top_view" />

        <androidx.recyclerview.widget.RecyclerView
            android:id="@+id/activity_shopping_cart_products_rv"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginStart="@dimen/marketplace_14dp"
            android:layout_marginTop="@dimen/marketplace_15dp"
            android:layout_marginEnd="@dimen/marketplace_14dp"
            app:layout_constraintTop_toBottomOf="@+id/activity_shopping_cart_top_view"
            tools:itemCount="4"
            tools:listitem="@layout/marketplace_cart_holder" />


    </LinearLayout>

    <LinearLayout
        android:id="@+id/activity_shopping_cart_products_summary_viewgroup"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_marginStart="@dimen/marketplace_14dp"
        android:layout_marginTop="@dimen/marketplace_15dp"
        android:layout_marginEnd="@dimen/marketplace_14dp"
        android:layout_marginBottom="@dimen/marketplace_15dp"
        android:layout_weight="0.45"
        android:orientation="vertical"
        app:layout_constraintTop_toBottomOf="@+id/activity_shopping_cart_rv_viewgroup">

        <View
            android:id="@+id/activity_shopping_cart_bottom_view"
            android:layout_width="match_parent"
            android:layout_height="1dp"
            android:layout_marginTop="@dimen/marketplace_15dp"
            android:background="@color/very_light_grey" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="@dimen/marketplace_15dp"
            android:text="@string/activity_shopping_cart_tax_0"
            android:textSize="18sp"
            android:textStyle="bold" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/activity_shopping_cart_tax_0"
            android:textSize="18sp"
            android:textStyle="bold" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="@dimen/marketplace_15dp"
            android:text="@string/activity_shopping_cart_all_amounts_are_in_usd"
            android:textSize="16sp" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/activity_shopping_cart_shipping_fees_calculated_at_checkout"
            android:textSize="16sp" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="@dimen/marketplace_15dp"
            android:text="@string/activity_shopping_cart_terms_and_policies"
            android:textSize="12sp"
            android:textStyle="bold" />

        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="@dimen/marketplace_15dp"
            android:background="@color/color_black"
            android:text="@string/activity_shopping_cart_proceed_to_checkout" />

        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="@dimen/marketplace_15dp"
            android:background="@drawable/shopping_cart__clear_background_button"
            android:textColor="@color/color_black"
            android:text="@string/activity_shopping_cart_continue_shopping" />


    </LinearLayout>

    </LinearLayout>


    </androidx.constraintlayout.widget.ConstraintLayout>
    </ScrollView>

你的布局不对。如果你有更多的项目,你要么必须使用 NestedScrollView,要么你应该重新设计你的布局,如下所示:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <LinearLayout
        android:id="@+id/top_layout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        app:layout_constraintTop_toTopOf="parent">

        <!-- Top Content -->

    </LinearLayout>


    <LinearLayout
        app:layout_constraintTop_toBottomOf="@+id/top_layout"
        app:layout_constraintBottom_toTopOf="@+id/bottom_layout"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:orientation="vertical">

        <!-- Other Content -->

    </LinearLayout>

    <LinearLayout
        android:id="@+id/bottom_layout"
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_constraintBottom_toBottomOf="parent">

        <!-- Bottom Content -->

    </LinearLayout>

</androidx.constraintlayout.widget.ConstraintLayout>

输出: