首页 > 解决方案 > 更改视图高度后 getY() 值不变

问题描述

我有一个框架布局,我根据不同的情况添加不同的高度视图。

我想知道添加视图前后框架布局的 Y 轴位置。

因此,在我添加视图之前,我调用getY()了返回 1891 的框架布局,这是正确的,一旦添加了新视图,它仍然返回 1891,这是错误的,它应该更少,因为框架布局在屏幕上更高。

我已经检查了这两种情况的布局检查器并getY()给出了正确的值,那为什么我的代码没有呢?

添加视图后,我尝试调用rootLayout.requestLayout()以重绘布局,但这不起作用。

    Log.d(TAG, "openActionBar: " + String.valueOf(actionFrameLayout.getY()));

    LayoutInflater.from(getContext()).inflate(R.layout.main_menu_3_item, frameLayout, true);

    questionRoot.requestLayout();

    Log.d(TAG, "openActionBar: " + String.valueOf(actionFrameLayout.getY()));

这是我的 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:id="@+id/questionRoot"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="?android:attr/colorBackground"
        android:clickable="true"
        android:focusable="true"
        tools:context=".QuestionFragments.Question">

        <com.englishquestionstogo.CustomViews.CustomChronometer
            android:id="@+id/timer"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="16dp"
            android:layout_marginEnd="16dp"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintTop_toTopOf="parent"
             />

        <ScrollView
            android:id="@+id/scrollView"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_marginTop="8dp"
            android:layout_marginBottom="8dp"
            app:layout_constraintBottom_toTopOf="@+id/actionFrameLayout"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="0.0"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent">

            <RelativeLayout
                android:id="@+id/relativeLayout"
                android:layout_width="match_parent"
                android:layout_height="wrap_content">

                <TextView
                    android:id="@+id/questionTitle"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginStart="16dp"
                    android:layout_marginTop="32dp"
                    android:layout_marginEnd="16dp"
                    android:textAlignment="center"
                    android:textSize="18sp"
                    android:textStyle="bold"
                    tools:text="Title" />

                <TextView
                    android:id="@+id/questionBody"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_below="@id/questionTitle"
                    android:layout_marginStart="16dp"
                    android:layout_marginTop="24dp"
                    android:layout_marginEnd="16dp"
                    android:layout_marginBottom="8dp"
                    android:paddingBottom="32dp"
                    android:textSize="16sp" />

            </RelativeLayout>

        </ScrollView>

        <View
            android:id="@+id/topBorder"
            android:layout_width="match_parent"
            android:layout_height="1dp"
            android:background="#D3D3D3"
            app:layout_constraintBottom_toTopOf="@id/frameLayout"
            />

        <View
            android:id="@+id/bottomBorder"
            android:layout_width="match_parent"
            android:layout_height="1dp"
            android:background="#D3D3D3"
            app:layout_constraintBottom_toBottomOf="parent"
            />


        <FrameLayout
            android:id="@+id/frameLayout"
            android:layout_width="match_parent"
            android:layout_height="48dp"
            android:background="@color/white"
            app:layout_constraintBottom_toTopOf="@id/bottomBorder"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent">

            <Button
                android:id="@+id/testButton"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                />


        </FrameLayout>


        <FrameLayout
            android:id="@+id/actionFrameLayout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="1.0"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintBottom_toTopOf="@id/frameLayout"
            >


        </FrameLayout>

    </androidx.constraintlayout.widget.ConstraintLayout>

标签: androidandroid-xml

解决方案


你应该使用addOnGlobalLayoutListener

 actionFrameLayout.getViewTreeObserver().addOnGlobalLayoutListener(
   new ViewTreeObserver.OnGlobalLayoutListener() {
      public void onGlobalLayout() {
           //Remove the listener before proceeding
           if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
                actionFrameLayout.getViewTreeObserver().removeOnGlobalLayoutListener(this);
           } else {
                actionFrameLayout.getViewTreeObserver().removeGlobalOnLayoutListener(this);
           }

           // get the absolute coordinates of a view
           Log.d(TAG, "openActionBar: " + String.valueOf(actionFrameLayout.getY()));

      }
   }
 );

推荐阅读