简体   繁体   English

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

[英]layout weight distribution is not as shown in XML preview

I have the following XML layout -我有以下 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>

which looks like this at the preview -在预览中看起来像这样 -

在此处输入图片说明

The issue I am facing is that for some reason, the bottom button "continue shopping" gets shrinked down -我面临的问题是,出于某种原因,底部的“继续购物”按钮缩小了 - 在此处输入图片说明

Even if I copy the same button downwards below it and replace it with the current one, the copied one will be shrinked to this size.即使我向下复制相同的按钮并将其替换为当前的按钮,复制的按钮也会缩小到这个大小。

Here is my clear_background_button.xml file -这是我的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>

What is causing the shrink?缩水的原因是什么?

Your layout is not right.你的布局不对。 Either you have to use NestedScrollView if you have more items or you should redesign your layout like below:如果您有更多项目,则必须使用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>

Output:输出:

在此处输入图片说明

try with this试试这个

<?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>

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

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