Представление ведет себя фиксированного размера вместо динамического размера на основе экрана с использованием ConstraintLayout - PullRequest
0 голосов
/ 03 июля 2019

Я пытаюсь создать экран настроек, ниже приведен код и предварительный просмотр в Android Studio.

У него есть несколько проблем.CardView с макетом scrollView не работает должным образом

Я хочу, чтобы мой CardView был настроен в середине userDetailsLayout и logoutBtn с интервалом @dimen/_20sdp.Но видно, что он растягивается вплоть до выхода из системы.

Если я использую мобильный телефон с небольшим экраном, 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 ]

1 голос
/ 03 июля 2019

Не на 100% уверен, что вы имеете в виду, но из кода я бы предложил вам изменить:

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

, когда вы указываете ограничения высоты и ширины для вида.В противном случае ограничения не будут работать.И кстати.Не используйте так много вложенных макетов внутри вашего ограничения.Каждому макету потребуется совершенно новый процесс измерения и рендеринга, который сильно замедляет работу вашего приложения.Пожалуйста, посмотрите также на Guidelines и ConstraintAspectRatio.

0 голосов
/ 03 июля 2019

Проблема в том, что logoutBtn находится в том же родительском элементе, что и ваш CardView.Они оба имеют

android:layout_margin="@dimen/_20sdp"

. Поэтому они имеют одинаковую ширину, они оба выровнены вправо и влево к родительскому элементу и имеют одинаковое значение поля.Поэтому, если вы хотите, чтобы это было 20dp от CardView, вы либо хотите вложить 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"
    />
...