使用ConstraintLayout,基于屏幕,视图的尺寸将变为固定尺寸,而不是动态尺寸

时间:2019-07-03 07:29:05

标签: java android android-studio android-constraintlayout

我正在尝试创建一个设置屏幕,以下是Android Studio中的代码和预览。

它有两个问题。带有scrollView布局的CardView无法正常工作

我想要的是,我的CardView将在userDetailsLayoutlogoutBtn的中间调整,间距为@dimen/_20sdp。但是可以看出,它一直延伸到logoutBtn,

如果我使用的是小屏幕手机,那么CardView也将开始重叠userDetailsLayout

就像,它使CardView的大小固定,而不是基于空间的大小可变。我该怎么办?

    android:layout_margin="@dimen/_20sdp"
    app:layout_constraintTop_toBottomOf="@+id/userDetailsLayout"
    app:layout_constraintBottom_toTopOf="@+id/logoutBtn"
    app:layout_constraintRight_toRightOf="parent"

enter image description here

<?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/forgot_bg_color"
        tools:context=".MainActivity">

    <com.google.android.material.appbar.AppBarLayout
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintLeft_toLeftOf="parent"
            android:id="@+id/topBar"
            android:layout_height="wrap_content"
            android:layout_width="match_parent"
            android:theme="@style/AppTheme.AppBarOverlay">

        <androidx.appcompat.widget.Toolbar
                app:contentInsetStart="0dp"
                android:elevation="5dp"
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="@dimen/_100sdp"
                android:background="@android:color/white"
                app:popupTheme="@style/AppTheme.PopupOverlay">

            <RelativeLayout
                    android:layout_marginBottom="@dimen/_5sdp"
                    android:layout_marginRight="@dimen/_10sdp"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent">

                <ImageView android:layout_width="@dimen/_35sdp"
                           android:layout_height="@dimen/_35sdp"
                           android:src="@drawable/down_arrow"
                           android:scaleType="centerInside"
                           android:rotation="90"
                           android:layout_marginBottom="-10dp"
                           android:layout_above="@+id/inboxBtn"
                           android:id="@+id/backBtn"
                />

                <TextView
                        android:layout_toRightOf="@+id/backBtn"
                        android:fontFamily="@font/montserrat_bold"
                        android:layout_above="@+id/inboxBtn"
                        android:gravity="bottom"
                        android:textSize="@dimen/_16sdp"
                        android:drawablePadding="@dimen/_8sdp"
                        android:text="@string/action_settings"
                        android:textColor="@color/text_gray"
                        android:id="@+id/homeTitle"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                />


                <TextView
                        android:id="@+id/inboxBtn"
                        android:fontFamily="@font/montserrat_medium"
                        android:layout_alignParentBottom="true"
                        android:textSize="@dimen/_14sdp"
                        android:gravity="center"
                        android:drawablePadding="@dimen/_8sdp"
                        android:textColor="@color/text_gray"
                        android:layout_width="wrap_content"
                        android:layout_height="@dimen/_35sdp"
                />


            </RelativeLayout>


        </androidx.appcompat.widget.Toolbar>

    </com.google.android.material.appbar.AppBarLayout>


    <RelativeLayout
            android:id="@+id/userDetailsLayout"
            android:paddingLeft="@dimen/_20sdp"
            android:paddingRight="@dimen/_20sdp"
            app:layout_constraintTop_toBottomOf="@+id/topBar"
            android:layout_width="match_parent"
            android:layout_height="@dimen/_100sdp"
    >


            <androidx.cardview.widget.CardView
                    android:id="@+id/userLayout"
                    android:layout_centerVertical="true"
                    app:cardElevation="5dp"
                    app:cardCornerRadius="@dimen/_35sdp"
                    android:layout_width="@dimen/_70sdp"
                    android:layout_height="@dimen/_70sdp"
            >

                <ImageView
                        android:id="@+id/userDp"
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                />


            </androidx.cardview.widget.CardView>

            <LinearLayout android:layout_width="match_parent"
                          android:layout_height="match_parent"
                          android:gravity="center_vertical"
                          android:orientation="vertical"
                          android:layout_toRightOf="@+id/userLayout"
                          android:layout_marginLeft="@dimen/_10sdp"
            >


                <TextView
                        android:fontFamily="@font/montserrat_bold"
                        android:textSize="@dimen/_16sdp"
                        android:text="@string/action_settings"
                        android:textColor="@color/text_gray"
                        android:id="@+id/title"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                />

                <TextView
                        android:fontFamily="@font/montserrat_regular"
                        android:textSize="@dimen/_12sdp"
                        android:text="@string/action_settings"
                        android:textColor="@color/text_gray"
                        android:id="@+id/details"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                />

            </LinearLayout>

    </RelativeLayout>

    <androidx.cardview.widget.CardView
            android:layout_margin="@dimen/_20sdp"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/userDetailsLayout"
            app:layout_constraintBottom_toTopOf="@+id/logoutBtn"
            android:layout_width="match_parent"
            app:cardCornerRadius="@dimen/_10sdp"
            app:cardElevation="0dp"
            android:layout_height="wrap_content"
    >

        <ScrollView
                android:layout_width="match_parent"
                android:layout_height="match_parent">

            <LinearLayout android:layout_width="match_parent"
                          android:layout_height="match_parent"
                          android:orientation="vertical"
                          android:paddingLeft="@dimen/_10sdp"
                          android:paddingRight="@dimen/_10sdp"
            >

                <RelativeLayout android:layout_width="match_parent"
                                android:layout_height="@dimen/_50sdp"
                >

                    <TextView
                            android:layout_marginLeft="@dimen/_5sdp"
                            android:layout_centerVertical="true"
                            android:fontFamily="@font/montserrat_regular"
                            android:textSize="@dimen/_12sdp"
                            android:text="@string/notifications"
                            android:textColor="@android:color/black"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                    />

                    <Switch android:layout_width="wrap_content"
                               android:layout_marginRight="@dimen/_5sdp"
                               android:layout_height="wrap_content"
                               android:layout_centerVertical="true"
                               android:layout_alignParentRight="true"
                    />


                </RelativeLayout>

                <View
                        android:layout_width="match_parent"
                        android:layout_height="1dp"
                        android:background="@color/light_gray"
                />

                <RelativeLayout android:layout_width="match_parent"
                                android:layout_height="@dimen/_50sdp"
                >

                    <TextView
                            android:layout_marginLeft="@dimen/_5sdp"
                            android:layout_centerVertical="true"
                            android:fontFamily="@font/montserrat_regular"
                            android:textSize="@dimen/_12sdp"
                            android:text="@string/manage_address"
                            android:textColor="@android:color/black"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                    />

                    <ImageView android:layout_width="wrap_content"
                               android:layout_marginRight="@dimen/_5sdp"
                               android:layout_height="wrap_content"
                               android:src="@drawable/right_arrow_small"
                               android:layout_centerVertical="true"
                               android:layout_alignParentRight="true"
                    />


                </RelativeLayout>

                <View
                        android:layout_width="match_parent"
                        android:layout_height="1dp"
                        android:background="@color/light_gray"
                />

                <RelativeLayout android:layout_width="match_parent"
                                android:layout_height="@dimen/_50sdp"
                >

                    <TextView
                            android:layout_marginLeft="@dimen/_5sdp"
                            android:layout_centerVertical="true"
                            android:fontFamily="@font/montserrat_regular"
                            android:textSize="@dimen/_12sdp"
                            android:text="@string/about"
                            android:textColor="@android:color/black"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                    />

                    <ImageView android:layout_width="wrap_content"
                               android:layout_marginRight="@dimen/_5sdp"
                               android:layout_height="wrap_content"
                               android:src="@drawable/right_arrow_small"
                               android:layout_centerVertical="true"
                               android:layout_alignParentRight="true"
                    />


                </RelativeLayout>

                <View
                        android:layout_width="match_parent"
                        android:layout_height="1dp"
                        android:background="@color/light_gray"
                />

                <RelativeLayout android:layout_width="match_parent"
                                android:layout_height="@dimen/_50sdp"
                >

                    <TextView
                            android:layout_marginLeft="@dimen/_5sdp"
                            android:layout_centerVertical="true"
                            android:fontFamily="@font/montserrat_regular"
                            android:textSize="@dimen/_12sdp"
                            android:text="@string/support"
                            android:textColor="@android:color/black"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                    />

                    <ImageView android:layout_width="wrap_content"
                               android:layout_marginRight="@dimen/_5sdp"
                               android:layout_height="wrap_content"
                               android:src="@drawable/right_arrow_small"
                               android:layout_centerVertical="true"
                               android:layout_alignParentRight="true"
                    />


                </RelativeLayout>

                <View
                        android:layout_width="match_parent"
                        android:layout_height="1dp"
                        android:background="@color/light_gray"
                />

                <RelativeLayout android:layout_width="match_parent"
                                android:layout_height="@dimen/_50sdp"
                >

                    <TextView
                            android:layout_marginLeft="@dimen/_5sdp"
                            android:layout_centerVertical="true"
                            android:fontFamily="@font/montserrat_regular"
                            android:textSize="@dimen/_12sdp"
                            android:text="@string/terms"
                            android:textColor="@android:color/black"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                    />

                    <ImageView android:layout_width="wrap_content"
                               android:layout_marginRight="@dimen/_5sdp"
                               android:layout_height="wrap_content"
                               android:src="@drawable/right_arrow_small"
                               android:layout_centerVertical="true"
                               android:layout_alignParentRight="true"
                    />


                </RelativeLayout>

                <View
                        android:layout_width="match_parent"
                        android:layout_height="1dp"
                        android:background="@color/light_gray"
                />

                <RelativeLayout android:layout_width="match_parent"
                                android:layout_height="@dimen/_50sdp"
                >

                    <TextView
                            android:layout_marginLeft="@dimen/_5sdp"
                            android:layout_centerVertical="true"
                            android:fontFamily="@font/montserrat_regular"
                            android:textSize="@dimen/_12sdp"
                            android:text="@string/privacy"
                            android:textColor="@android:color/black"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                    />

                    <ImageView android:layout_width="wrap_content"
                               android:layout_marginRight="@dimen/_5sdp"
                               android:layout_height="wrap_content"
                               android:src="@drawable/right_arrow_small"
                               android:layout_centerVertical="true"
                               android:layout_alignParentRight="true"
                    />


                </RelativeLayout>

                <View
                        android:layout_width="match_parent"
                        android:layout_height="1dp"
                        android:background="@color/light_gray"
                />

            </LinearLayout>


        </ScrollView>

    </androidx.cardview.widget.CardView>

    <TextView
            android:id="@+id/logoutBtn"
            android:fontFamily="@font/montserrat_bold"
            android:gravity="center"
            android:layout_marginRight="@dimen/_20sdp"
            android:layout_marginLeft="@dimen/_20sdp"
            android:layout_marginBottom="@dimen/_20sdp"
            android:layout_width="match_parent"
            android:layout_height="@dimen/_40sdp"
            android:textColor="@android:color/white"
            android:text="@string/logout"
            android:textSize="@dimen/_14sdp"
            android:background="@drawable/logout_bg"
            app:layout_constraintBottom_toBottomOf="parent"

    />


</androidx.constraintlayout.widget.ConstraintLayout>

2 个答案:

答案 0 :(得分:1)

不能100%确定您的意思,但是我建议您从代码中进行更改:

    android:layout_width="0dp"
    android:layout_height="0dp"

在视图上指定高度和宽度约束时。否则约束将不起作用。顺便说一句。不要在constraintlayout内部使用太多嵌套的布局。每个布局都需要一个全新的度量和渲染过程,这会使您的应用速度大大降低。另请参阅GuidelinesConstraintAspectRatio

答案 1 :(得分:0)

问题在于logoutBtnCardView在同一父级中。他们俩都有

android:layout_margin="@dimen/_20sdp"

这就是为什么它们具有相同的宽度,它们都与父对象左右对齐,并且具有相同的边距。因此,如果您希望它是CardView 20dp ,则可以将logoutBtn嵌套在卡片视图中,或者只是使logoutBtn具有 40dp

<TextView
    android:id="@+id/logoutBtn"
    android:layout_width="0dp"
    android:layout_height="40dp"
    android:layout_marginLeft="40dp"
    android:layout_marginRight="40dp"
    android:layout_marginBottom="20dp"
    android:background="@color/error_red"
    android:gravity="center"
    android:text="string/logout"
    android:textColor="@android:color/white"
    android:textSize="14sp"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    />