简体   繁体   English

ConstraintLayout 中基于百分比的指南在视图可见时移动,android

[英]Percentage based guideline in ConstraintLayout moves when view is made visible, android

Context : I am making part of a sign-up page.上下文:我正在制作注册页面的一部分。 Once the user types in their email, they tap continue and type in their password.一旦用户输入他们的 email,他们点击继续并输入他们的密码。 Tapping continue triggers Kotlin code that sets the password view's visibility to VISIBLE.点击 continue 会触发 Kotlin 代码,该代码将密码视图的可见性设置为 VISIBLE。

The top TextView is constrained to a guideline with the attribute "app:layout_constraintGuide_percent=".3". I expect this to mean that it will never move.顶部 TextView 受限于属性“app:layout_constraintGuide_percent=".3"。我希望这意味着它永远不会移动。

Problem : When the password input view is set to visible by the Kotlin code, all of the views shift downwards, inexplicably.问题:当密码输入视图通过 Kotlin 代码设置为可见时,所有视图都莫名其妙地向下移动。 It is as if the 30% becomes 35%, although of course that is not the case.就好像 30% 变成了 35%,当然事实并非如此。

I have checked out this post and tried to use bias, but it generates the exact same behaviour.我查看了这篇文章并尝试使用偏见,但它会产生完全相同的行为。

fragment_sign_up_email.xml: fragment_sign_up_email.xml:

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/white"
        android:padding="@dimen/sign_up_in_outer_padding"
        android:animateLayoutChanges="true"
        tools:context=".SignUpEmailFragment">


        <TextView
            android:id="@+id/textView4"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:fontFamily="@font/lato_bold"
            android:text="@string/xxxx"
            android:textSize="@dimen/title_font_size"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="@+id/guideline3"/>

        <TextView
            android:id="@+id/textView5"
            android:layout_width="20dp"
            android:layout_height="wrap_content"
            android:fontFamily="@font/lato"
            android:text="@string/or"
            android:textSize="@dimen/subtitle_font_size"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/textView4" />

        <TextView
            android:id="@+id/sign_in_button_on_sign_up_screen"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:fontFamily="@font/lato_bold"
            android:text="@string/sign_in"
            android:textColor="@color/main_green"
            android:textSize="@dimen/subtitle_font_size"
            app:layout_constraintStart_toEndOf="@+id/textView5"
            app:layout_constraintTop_toBottomOf="@+id/textView4"/>

        <com.google.android.material.textfield.TextInputLayout
            android:id="@+id/email_field_sign_up"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="30dp"
            android:hint="@string/email_hint"
            app:boxBackgroundColor="@color/white"
            app:errorEnabled="true"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/sign_in_button_on_sign_up_screen"
            app:startIconDrawable="@drawable/common_google_signin_btn_icon_light">

            <com.google.android.material.textfield.TextInputEditText
                android:id="@+id/email_input_sign_up"
                android:layout_width="match_parent"
                android:layout_height="wrap_content" />

        </com.google.android.material.textfield.TextInputLayout>

        <com.google.android.material.textfield.TextInputLayout
            android:id="@+id/password_field_sign_up"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="@string/password_hint"
            app:boxBackgroundColor="@color/white"
            android:visibility="gone"
            app:errorEnabled="true"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/email_field_sign_up"
            app:startIconDrawable="@drawable/common_google_signin_btn_icon_light">

            <com.google.android.material.textfield.TextInputEditText
                android:id="@+id/password_input_sign_up"
                android:layout_width="match_parent"
                android:layout_height="wrap_content" />

        </com.google.android.material.textfield.TextInputLayout>

        <Button
            android:id="@+id/sign_up_email_pass_continue"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="@string/continue_text"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/password_field_sign_up" />

        <androidx.constraintlayout.widget.Guideline
            android:id="@+id/guideline3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            app:layout_constraintGuide_percent=".3" />


    </androidx.constraintlayout.widget.ConstraintLayout>



</layout>

SignUpEmailFragment.kt: SignUpEmailFragment.kt:

package com.example.xxxxx

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.databinding.DataBindingUtil
import androidx.fragment.app.Fragment
import com.example.xxxxx.databinding.FragmentSignUpEmailBinding

/**
 * Fragment that handles the sign in procedure
 */
class SignUpEmailFragment : Fragment() {

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,savedInstanceState: Bundle?): View? {
        val binding: FragmentSignUpEmailBinding = DataBindingUtil.inflate(inflater,R.layout.fragment_sign_up_email  , container, false)

        binding.signUpEmailPassContinue.setOnClickListener{view: View ->
            continueIsClicked(binding)
        }

        return binding.root
    }


    private fun continueIsClicked(binding: FragmentSignUpEmailBinding) {
        if(emailIsValid(binding)){
                binding.passwordFieldSignUp.setVisibility(View.VISIBLE);
                if(passwordIsValid(binding)){
                    //TODO navigate to next page
                }
        }
    }

    private fun passwordIsValid(binding: FragmentSignUpEmailBinding): Boolean {
        //TODO implement
        return true
    }

    private fun emailIsValid(binding: FragmentSignUpEmailBinding): Boolean {
        //TODO implement
        return true
    }

    companion object {
        fun newInstance() = SignUpEmailFragment()
    }

}

Before click: screenshot 1点击前:截图1

After click: screenshot 2点击后:截图2

I figured it out.我想到了。

The androidx.fragment.app.FragmentContainerView in which this Fragment appears had the attribute android:layout_height="wrap_content" .出现此 Fragment 的androidx.fragment.app.FragmentContainerView具有属性android:layout_height="wrap_content" I changed it to android:layout_height="match_parent" and the it worked correctly.我将其更改为android:layout_height="match_parent"并且它工作正常。

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

相关问题 ConstraintLayout准则与视图一起扩展 - ConstraintLayout Guideline expanding together with view ConstraintLayout:基于百分比的维度 - ConstraintLayout: percentage based dimensions 根据ConstraintLayout中另一个视图的高度更改高度百分比 - Change height percentage based on another view's height in ConstraintLayout 使应用在设备中运行时,ConstraintLayout中的视图未按预期放置 - A View in ConstraintLayout not being placed as expected when app is made run in device android ConstraintLayout高度百分比0.99 - android ConstraintLayout height percentage 0.99 Android - 使用尺寸的 ConstraintLayout 百分比 - Android - ConstraintLayout percentage using dimens ConstraintLayout:在工具栏上重叠ImageView并根据GuideLine的宽度设置图像高度 - ConstraintLayout: Overlap ImageView on Toolbar and set image Height based on width by GuideLine 指南不会根据 ConstraintLayout 中障碍内的视图尺寸的变化而变化 - Guideline does not shift based on change in Views dimensions inside barrier in ConstraintLayout Android Studio 指南 header 百分比不起作用 - Android Studio guideline header percentage not working ConstraintLayout Barrier 在设计视图中不可见 - ConstraintLayout Barrier not visible in design view
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM