首页 > 解决方案 > getY 在片段中使用时返回 0.0

问题描述

我已经在这里尝试过答案:View getX() and getY() return 0.0 after they have been added to the Activity

private float foundHeight;
foundationZero = (ImageView) rootView.findViewById(R.id.foundation_zero);
        foundationZero.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                // Encuentra la posición de las found para comenzar a posicionar los demás elementos
                foundHeight = foundationZero.getY();
                foundationZero.getViewTreeObserver().removeOnGlobalLayoutListener(this);
                System.out.println("HEIGHT ---->: " + foundHeight);}
        });

XML 基础是:

<FrameLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/root_fragment_view"
    android:layout_width="match_parent" android:layout_height="match_parent"
    android:orientation="vertical">
    <RelativeLayout
        android:id="@+id/gameInformationLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentStart="true"
            android:layout_alignParentLeft="true"
            android:layout_marginLeft="@dimen/informationMargins"
            android:layout_marginTop = "@dimen/informationMargins"
            android:text="@string/scoreString"/>
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:layout_marginTop = "@dimen/informationMargins"
            android:text="@string/movString"/>

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentEnd="true"
            android:layout_alignParentRight="true"
            android:layout_marginTop = "@dimen/informationMargins"
            android:layout_marginRight="@dimen/informationMargins"
            android:text="@string/timeString"/>
    </RelativeLayout>

    <!-- Static Layout -->

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="30dp">

        <ImageView
            android:id="@+id/redeal_imageview"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginStart="@dimen/foundationBetweenCardMargin"
            android:layout_marginLeft="@dimen/foundationBetweenCardMargin"
            android:adjustViewBounds="true"
            android:maxWidth="@dimen/maxWidthCard"
            android:maxHeight="@dimen/maxHeightCard"
            android:scaleType="centerCrop"
            android:src = "@drawable/b1fv"/>
        <ImageView
            android:id="@+id/foundation_zero"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginEnd="@dimen/foundationBetweenCardMargin"
            android:layout_marginRight="@dimen/foundationBetweenCardMargin"
            android:adjustViewBounds="true"
            android:maxWidth="@dimen/maxWidthCard"
            android:maxHeight="@dimen/maxHeightCard"
            android:scaleType="centerCrop"
            android:layout_toStartOf="@+id/foundation_one"
            android:layout_toLeftOf="@+id/foundation_one" />
        <ImageView
            android:id="@+id/foundation_one"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginEnd="@dimen/foundationBetweenCardMargin"
            android:layout_marginRight="@dimen/foundationBetweenCardMargin"
            android:adjustViewBounds="true"
            android:maxWidth="@dimen/maxWidthCard"
            android:maxHeight="@dimen/maxHeightCard"
            android:scaleType="centerCrop"
            android:layout_toStartOf="@+id/foundation_two"
            android:layout_toLeftOf="@+id/foundation_two" />
        <ImageView
            android:id="@+id/foundation_two"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginEnd="@dimen/foundationBetweenCardMargin"
            android:layout_marginRight="@dimen/foundationBetweenCardMargin"
            android:adjustViewBounds="true"
            android:maxWidth="@dimen/maxWidthCard"
            android:maxHeight="@dimen/maxHeightCard"
            android:scaleType="centerCrop"
            android:layout_toStartOf="@+id/foundation_three"
            android:layout_toLeftOf="@+id/foundation_three" />
        <ImageView
            android:id="@+id/foundation_three"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentEnd="true"
            android:layout_alignParentRight="true"
            android:adjustViewBounds="true"
            android:maxWidth="@dimen/maxWidthCard"
            android:maxHeight="@dimen/maxHeightCard"
            android:scaleType="centerCrop"
            android:layout_marginRight="@dimen/foundationBetweenCardMargin" />


    </RelativeLayout>


</FrameLayout>

返回 0.0。我需要这些图像视图的位置,因为我正在以编程方式添加更多视图。从代码中可以看出,我已经尝试了 URL 中的解决方案,但它不起作用。顺便说一句,我在标题中添加了“用于片段”,因为上面的代码写在片段中的事实可能与它不起作用的事实有关。

标签: android

解决方案


View.getY()不会给你视图的高度,而是Y容器内视图左上角的位置。相反,您想使用View.getHeight(),它将以像素为单位返回视图的高度(不是 DP!)。


推荐阅读