Центр прокрутки div для Android в вертикальном положении

Я возился, пытаясь сохранить свое поле входа по центру по вертикали, и по какой-то причине это не работает. Если я изменю высоту своего ConstraintLayout на wrap_content, форма входа будет центрирована, но фоновое изображение также будет сжато следующим образом:

Центр прокрутки div для Android в вертикальном положении

Фоновое изображение должно оставаться в полноэкранном режиме, но поскольку основной макет ограничения представляет собой перенос содержимого, он сжимает свой фон. Вот что произойдет, если я установлю его на fill_parent. Получаем полноэкранное изображение, но и блок уже не по центру.

Центр прокрутки div для Android в вертикальном положении

Сейчас у меня такая структура:

Constraint Layout
  -> Linear Layout
    -> Scroll View
      -> login form picture, fields, and button

Я настраиваю их все на обертку, используя gravity И layout_gravity для center, center_vertical и т. д., И я все еще не могу центрировать это изображение. Какие-либо предложения? Вот текущий XML для моего макета activity_login.

<?xml version = "1.0" encoding = "utf-8"?>
<android.support.constraint.ConstraintLayout
    xmlns:android = "http://schemas.android.com/apk/res/android"
    xmlns:app = "http://schemas.android.com/apk/res-auto"
    android:layout_width = "fill_parent"
    android:layout_height = "fill_parent"
    android:orientation = "vertical"
    android:gravity = "center"
    android:layout_gravity = "center"
    android:background = "@drawable/background"
    android:weightSum = "1">
    <LinearLayout
        android:layout_width = "match_parent"
        android:layout_height = "wrap_content"
        android:layout_gravity = "center"
        android:gravity = "fill_vertical|center"
        android:orientation = "vertical"
        android:padding = "46dp">
        <ScrollView
            android:layout_width = "match_parent"
            android:layout_height = "wrap_content"
            android:layout_gravity = "center"
            android:gravity = "center">
            <LinearLayout
                android:id = "@+id/email_login_form"
                android:layout_width = "match_parent"
                android:layout_height = "wrap_content"
                android:layout_gravity = "center"
                android:descendantFocusability = "beforeDescendants"
                android:focusableInTouchMode = "true"
                android:gravity = "center"
                android:orientation = "vertical">
                <ImageView
                    android:id = "@+id/imageView"
                    android:layout_width = "match_parent"
                    android:layout_height = "wrap_content"
                    android:contentDescription = "@string/login_image"
                    app:srcCompat = "@drawable/login_logo" />
                <android.support.design.widget.TextInputLayout
                    android:layout_width = "match_parent"
                    android:layout_height = "wrap_content"
                    android:layout_marginTop = "30dp">
                    <AutoCompleteTextView
                        android:id = "@+id/email"
                        android:layout_width = "match_parent"
                        android:layout_height = "wrap_content"
                        android:background = "@color/formbg"
                        android:ems = "10"
                        android:hint = "@string/prompt_email"
                        android:imeActionId = "1"
                        android:imeOptions = "actionNext"
                        android:inputType = "textEmailAddress"
                        android:maxLines = "1"
                        android:padding = "16dp" />
                </android.support.design.widget.TextInputLayout>
                <android.support.design.widget.TextInputLayout
                    android:layout_width = "match_parent"
                    android:layout_height = "wrap_content">
                    <EditText
                        android:id = "@+id/password"
                        android:layout_width = "match_parent"
                        android:layout_height = "wrap_content"
                        android:background = "@color/formbg"
                        android:hint = "@string/prompt_password"
                        android:imeActionId = "2"
                        android:imeActionLabel = "@string/action_sign_in_short"
                        android:imeOptions = "actionUnspecified"
                        android:inputType = "textPassword"
                        android:maxLines = "1"
                        android:padding = "16dp" />
                </android.support.design.widget.TextInputLayout>
                <Button
                    android:id = "@+id/email_sign_in_button"
                    style = "?android:textAppearanceSmall"
                    android:layout_width = "match_parent"
                    android:layout_height = "wrap_content"
                    android:layout_marginTop = "30dp"
                    android:background = "#9f3737"
                    android:text = "@string/action_sign_in"
                    android:textColor = "#ffffff"
                    android:textStyle = "bold" />
            </LinearLayout>
        </ScrollView>
    </LinearLayout>
</android.support.constraint.ConstraintLayout>

Возможный дубликат Как центрировать элементы в ConstraintLayout

grrigore 16.11.2018 23:21
0
1
29
1
Перейти к ответу Данный вопрос помечен как решенный

Ответы 1

Ответ принят как подходящий

Добавление этого к дочернему элементу ConstraintLayout должно центрировать дочерний элемент как по горизонтали, так и по вертикали:

app:layout_constraintLeft_toLeftOf = "parent"
app:layout_constraintRight_toRightOf = "parent"
app:layout_constraintTop_toTopOf = "parent"
app:layout_constraintBottom_toBottomOf = "parent"

Также обратите внимание, что: fill_parent был переименован в match_parent в API уровня 8 и выше.

Другие вопросы по теме