【问题标题】:layout weight distribution is not as shown in XML preview布局权重分布未如 XML 预览中所示
【发布时间】:2020-01-08 08:49:34
【问题描述】:

我有以下 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>

在预览中看起来像这样 -

我面临的问题是,由于某种原因,底部的“继续购物”按钮被缩小了 -

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

这是我的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>

是什么导致了收缩?

【问题讨论】:

  • 您添加了更多不适合设备屏幕的内容。尝试使用scrollview

标签: android android-linearlayout android-button android-layout-weight


【解决方案1】:

你的布局不对。如果您有更多项目,您必须使用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>

输出:

【讨论】:

    【解决方案2】:

    试试这个

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

    【讨论】:

    • 我想在一个屏幕上保留所有内容 - 我不需要滚动视图。
    • 那么要么使用线性布局作为父布局并使用权重,要么使用 ConstraintLayout 内部的约束来指定每个子高度的百分比
    • 嗯...这与我现在所做的实际上有何不同?
    • 我的意思是为每个孩子设置权重并删除所有不必要的父布局或使用 ConstraintLayout 的指南,它可以为其中的每个孩子提供百分比高度
    猜你喜欢
    • 1970-01-01
    • 2015-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-05-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多